void menu(){
System.out.println("1. Take Quiz");
System.out.println("2. Quiz Results");
System.out.println("3. Performance Comment");
System.out.println("4. Exit");
System.out.print("Choose from the above:");
byte menu = Keyboard.readByte();
switch(menu){
case 1 :
takeQuiz();
break;
case 2 :
quizResults();
break;
case 3 :
performanceComment();
break;
case 4 :
exit();
}
}
我想制作这个代码循环,这样如果用户输入案例1,则会出现案例1,但之后整个菜单会再次出现。与案例2等相同 我应该使用循环,如果是,如何? 任何帮助表示赞赏。
答案 0 :(得分:0)
在begin和while循环中添加一个布尔标志,直到用户选择4:
void menu(){
boolean mustRun = true;
while(mustRun){
System.out.println("1. Take Quiz");
System.out.println("2. Quiz Results");
System.out.println("3. Performance Comment");
System.out.println("4. Exit");
System.out.print("Choose from the above:");
byte menu = Keyboard.readByte();
switch(menu){
case 1 :
takeQuiz();
break;
case 2 :
quizResults();
break;
case 3 :
performanceComment();
break;
case 4 :
mustRun = false;
break;
}
}
}
答案 1 :(得分:0)
您可以同时使用 或用于循环:
while循环:
void menu() {
while (true) {
System.out.println("1. Take Quiz");
System.out.println("2. Quiz Results");
System.out.println("3. Performance Comment");
System.out.println("4. Exit");
System.out.print("Choose from the above:");
byte menu = Keyboard.readByte();
switch (menu) {
case 1:
takeQuiz();
case 2:
quizResults();
case 3:
performanceComment();
case 4:
break;
}
}
}
for循环:
void menu() {
for (;;) {
System.out.println("1. Take Quiz");
System.out.println("2. Quiz Results");
System.out.println("3. Performance Comment");
System.out.println("4. Exit");
System.out.print("Choose from the above:");
byte menu = Keyboard.readByte();
switch (menu) {
case 1:
takeQuiz();
case 2:
quizResults();
case 3:
performanceComment();
case 4:
break;
}
}
}
两种方式都使用无限循环
Break关键字终止循环。
Here您可以阅读有关break关键字的更多信息
希望它有所帮助。