A randomly selected students to answer questions

| - Demand: In a small class, of all people, randomly selected person to answer a question

 

| - realization of ideas:

1, all students enter the name of an array

2, write a statement that generates random integer

3, let the random integer where the name of the act as an array index, and outputs a corresponding name

 

Code content:

the Try Package; 

Import java.util.Scanner;

/ **
* @auther Zhou Xiaolong ::
* @Description: a random name
* @program: Xunhuan
* @Create: 2019-07-03 14:59
* /
public class {the Random
Scanner Scanner SC = new new static (the System.in);
public static void main (String [] args) {
// definition array, storing names
// Si may Hsuan Meng Xuan Lan Lan Zeng Tao Beecham Xiaotian Lin Peng Dajiuhu with a view dragons
String [] names = { "nine lakes", "Jenny," "can Xuan", "Da Xuan", "Meng thinking", "Lan Lan", "Zeng Tao," "will become", "laugh day "," Lin Peng "," order "," dragons "," the first floor of Heaven "};
System.out.println (" Please press 0 a random draw students to answer questions ");
// set to receive a keyboard variable information
int key = sc.nextInt ();
the while (Key == 0) {
// System.out.println ( "start random named ******* ********");
// set a 0
int Random = (int) (Math.random () * 13-1);
for (int I = 0; I <of names.length; I ++) {
IF (Random == I) {
the System.out .println ( "Please ---" + names [i] + "--- answered questions");
BREAK;
}

}
// System.out.println ( "\ n-\ n-start random ******* named ******** ");
System.out.println (" \ the n-\ 0 the n-click a random draw students to answer questions ");
Key = sc.nextInt ();
}
}
}


| - operation result

 






Guess you like

Origin www.cnblogs.com/twuxian/p/11126824.html