[英]How to return to main menu with switch cases
I need to return to main menu when 3 is selected. 选择3时,我需要返回主菜单。 My main menu code is another loop that I have not included as code but I just want to return to it.(The first loop is in main).
我的主菜单代码是另一个循环,我没有作为代码包含在内,但我只想返回它。(第一个循环在main中)。
Here is my code: 这是我的代码:
public static void addEvent() {
while (true) {
boolean valid = false;
do {
Scanner sc = new Scanner(System.in);
System.out.println("What type of event is it?");
System.out.println("Lecture = 1. \nWorkshop = 2. \nReturn to Main Menu = 3. \nExit Program = 4. \nINPUT : ");
int action = sc.nextInt();
valid = true;
switch (action) {
case 1:
valid = true;
lectureEvent();
break;
case 2:
valid = true;
workshopEvent();
break;
case 3:
valid = true;
break;
case 4:
valid = true;
return;
default:
valid = false;
System.out.println("ERROR : Choice " + action + "Does not exist.");
System.out.println("Please choose an alternative.");
}
} while (!valid);
}
}
Do it like you did it in case 4, replace the break for a return
像在情况4中一样进行操作,请更换中断以
return
switch (action) {
case 1:
valid = true;
lectureEvent();
break;
case 2:
valid = true;
workshopEvent();
break;
case 3:
valid = true;
return;
case 4:
valid = true;
return;
default:
valid = false;
System.out.println("ERROR : Choice " + action + "Does not exist.");
System.out.println("Please choose an alternative.");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.