在下面的switch语句的情况5中,我希望用户输入数组中的一个学生,并从模块数组中选择一个模块来注册它们而不重复。任何想法/例子都会非常有用。提前致谢。在Java中添加新元素而不重复Java
控制类:
import java.util.Scanner;
public class Control {
public void run() {
while (true) {
Menu menu = new Menu();
menu.getMainMenu();
try {
Scanner scan = new Scanner(System.in);
int selection = scan.nextInt();
switch (selection) {
case 1:
for (Student student : students) {
System.out.print(student.getName() + " ");
}
break;
case 2:
for (Module module : modules) {
System.out.print(module.getName() + " ");
}
break;
case 3:
...
case 4:
...
case 5:
// Print out students
System.out.println("select a student: ");
for (int i = 0; i < students.length; i++) {
System.out.println(i + " " + students[i]);
}
selection = scan.nextInt();
############################
Confusion here
############################
case 6:
System.out.println("Goodbye!");
System.exit(0);
break;
default:
System.out.println("Invalid option selected. You must enter a number between 1 & 6!");
} // end switch
} catch (Exception e) {
System.out.println("Invalid entry. You must enter a number between 1 & 6");
}
} // end while
}
}
很好的答案。 LOL比我的简洁得多! –