有人可以告诉我如何让这个代码在按下4时返回主菜单。我知道我必须做while循环,但我不知道如何。现在我退出作为返回,所以它关闭整个程序,但我希望它返回主菜单并重新启动eventSelection。
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 and 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.");
}
}
答案 0 :(得分:0)
你可以使用这样的无限循环来执行此操作,但是用户将无法完全退出程序,如果要命令退出程序,则可以在该命令中使用 public static void eventSelection()
{
while (true){
Scanner sc = new Scanner(System.in);
System.out.println("Select Scheduling Action");
System.out.print("Add and 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)
{
System.out.println("Returning to selection menu");// Do nothing, next loop will be executed
}
}
else
{
System.out.println("Error : Choice " + actionSelect + " Does Not Exist.");
}
}
。
On Error GoTo ErrorHandler
}
答案 1 :(得分:0)
从eventSelection()返回一些值。检查main函数返回的值。如果它与你的值匹配,则再次调用eventSelection()