[英]How to return to main menu when exit is inputted
I want to return to main menu aka "eventSelection" if 4 (exit) is selected.如果选择了 4(退出),我想返回主菜单又名“eventSelection”。 Right now I have it as return which exits whole program.
现在我把它作为退出整个程序的回报。
import java.util.*;
public class SchedulingProgram {
public static void main (String [] args) {
eventSelection();
}
public static void eventSelection() {
Scanner sc = new Scanner(System.in);
System.out.println("Select Scheduling Action");
System.out.print("Add an Event = 1. \nDisplay Events = 2.\nPrint Alert = 3. \nExit = 4. \nINPUT : ");
int actionSelect = sc.nextInt();
if (actionSelect >= 1 && actionSelect <= 4) {
if (actionSelect == 1) {
addEvent();
} else if (actionSelect == 2) {
displayEvent();
} else if (actionSelect == 3) {
printAlert();
} else if (actionSelect == 4) {
return;
}
} else {
System.out.println("Error : Choice " + actionSelect + " Does Not Exist.");
}
}
I used to play with these kind of things too when I first started learning Java.当我第一次开始学习 Java 时,我也曾经玩过这些东西。 The most simple solution is indeed a while-loop.
最简单的解决方案确实是一个while循环。 It evaluates the boolean expression and keeps executing the code within its brackets for as long as the boolean equals 'true'.
只要布尔值等于“真”,它就会计算布尔表达式并继续执行括号内的代码。 I refactored your example:
我重构了你的例子:
public static void eventSelection() {
Scanner sc = new Scanner(System.in);
int actionSelect = 0;
while (actionSelect != 4) {
System.out.println("Select Scheduling Action");
System.out.print("Add an Event = 1. \nDisplay Events = 2.\nPrint Alert = 3. \nExit program = 4. \nINPUT : ");
actionSelect = sc.nextInt();
switch (actionSelect) {
case 1:
addEvent();
break;
case 2:
displayEvent();
break;
case 3:
printAlert();
break;
default:
break;
}
}
System.out.println("Exited program");
}
It will now display your menu everytime a number is entered.现在,每次输入数字时,它都会显示您的菜单。 Except when they enter 4, which now becomes the exit out of your entire program.
除非他们输入 4,它现在变成了整个程序的出口。
When faced with multiple if-else, it's perhaps better to use a switch-case.当面对多个 if-else 时,最好使用 switch-case。 It takes a parameter and per case, you can define the action.
它需要一个参数,您可以根据情况定义操作。 If the entered parameter doesn't match a case, it falls back to the default (in this case, it does nothing and the program will continue, showing the select menu anyway)
如果输入的参数与大小写不匹配,它会回退到默认值(在这种情况下,它什么都不做,程序将继续,无论如何都会显示选择菜单)
The 'break' keyword is needed to contain the cases.需要'break'关键字来包含案例。 Otherwise, the next line of code would be executed as well.
否则,下一行代码也会被执行。 A bit more information about the switch statement can be found here.
可以在此处找到有关 switch 语句的更多信息。
And ofcourse, here's the link to the while-loops.当然,这里是while 循环的链接。
One last tip... As I learned to program, I found that there's nothing more important than to (re)search as much as I could on Google.最后一个提示... 当我学习编程时,我发现没有什么比在 Google 上尽可能多地(重新)搜索更重要的了。 There are examples aplenty out there.
那里有很多例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.