实验题目:
public class Main {
public static void main(String[] args) {
System.out.println("-----随机点名器-----");
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要存储学生的人数:");
int n = sc.nextInt();
String[] students = new String[n];
addName(students);
printName(students);
randomName(students);
}
public static void addName(String[] students) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < students.length; i++) {
System.out.println("存储第"+(i+1)+"个姓名:");
students[i] = sc.next();
}
}
public static void printName(String[] students) {
for (int i = 0; i < students.length; i++) {
System.out.println("第"+(i+1)+"个学生姓名:"+students[i]);
}
}
public static void randomName(String[] students) {
Random random = new Random();
int index = random.nextInt(students.length);
String name = students[index];
System.out.println("被点到名的同学是:"+name);
}
}