Java 从嵌套的switch语句退出到外部switch语句
我用Java创建了一个菜单,其中每个选项都有另一个菜单。我想知道我是否可以从内部菜单退出回到主菜单 编辑:添加了主菜单选项Java 从嵌套的switch语句退出到外部switch语句,java,nested,switch-statement,Java,Nested,Switch Statement,我用Java创建了一个菜单,其中每个选项都有另一个菜单。我想知道我是否可以从内部菜单退出回到主菜单 编辑:添加了主菜单选项 System.out.println("Main Menu"); System.out.println("1. Hourly Employees"); System.out.println("2. Salary Employees"); System.out.println("3. Commission Employees"); System
System.out.println("Main Menu");
System.out.println("1. Hourly Employees");
System.out.println("2. Salary Employees");
System.out.println("3. Commission Employees");
System.out.println("4. Weekly Calculations");
System.out.println("5. Exit Program");
while(true){
switch(choice){
case 1:
HourlyEmployeeChoice HEC = new HourlyEmployeeChoice();
//HourlyEmployeeChoice is a class with HEmployeeMenu() method
HEC.HEmployeeMenu();
break;
//case 2-4: to be added later
case 5:
System.out.println("Goodbye!");
System.exit(0);
}
}
这是我的主菜单。我只为第一个选项和退出选项编写了代码
public void HEmployeeMenu(){
while(true){
//submenu options
int hourlyChoice = userChoice.nextInt();
switch(hourlyChoice) {
case 1:
//code
break;
case 2:
//code
break;
case 3:
//code
break;
case 4: //exit submenu
return;
default:
System.out.println("Please make a valid selection");
}
}
}
这是我放入第二个switch语句的方法。选择选项4(退出子菜单)将刷新到子菜单而不是主菜单。我想这是因为我对子菜单使用了while循环。但我不想取消这个选项,因为我希望在完成案例1-3后能够继续做出选择 我相信你的问题是
while(true){
// HERE
switch(choice){
您需要添加代码才能再次获得选项
,否则它将始终是您第一次输入的内容
也就是说,将主菜单显示和选项
输入移动到类似的位置
while(true){
System.out.println("Main Menu"); // <-- Added based on your edit.
System.out.println("1. Hourly Employees");
System.out.println("2. Salary Employees");
System.out.println("3. Commission Employees");
System.out.println("4. Weekly Calculations");
System.out.println("5. Exit Program");
int choice = userChoice.nextInt(); // <-- HERE
switch(choice){
while(true){
System.out.println(“主菜单”);//我相信您遇到的问题是
while(true){
// HERE
switch(choice){
您需要添加代码才能再次获得选项
,否则它将始终是您第一次输入的内容
也就是说,将主菜单显示和选项
输入移动到类似的位置
while(true){
System.out.println("Main Menu"); // <-- Added based on your edit.
System.out.println("1. Hourly Employees");
System.out.println("2. Salary Employees");
System.out.println("3. Commission Employees");
System.out.println("4. Weekly Calculations");
System.out.println("5. Exit Program");
int choice = userChoice.nextInt(); // <-- HERE
switch(choice){
while(true){
System.out.println(“主菜单”);//谢谢您的快速回复!这种方式很有效。当我退出子菜单并返回主菜单时,主菜单的选项不会再次出现。我可以键入一个数字(1-5)并从主菜单中获取该数字的命令,但我希望主菜单选项在退出子菜单后再次出现。@SeverelyConfused也许您应该添加该显示。您没有将其发布在此处,但它可能会在while(true)
之后和int choice=userChoice.nextInt()之前出现这正是我想要的。我以前试过,但没用。我想我一定是把主菜单选项放在int选项之后了。谢谢!谢谢你的快速回复!这种方法很有效。当我退出子菜单并返回主菜单时,主菜单的选项不会再出现。我可以键入一个数字(1-5)并从主菜单中获取该数字的命令,但我希望主菜单选项在退出子菜单后再次出现。@SeverelyConfused也许您应该添加该显示。您没有将其发布在此处,但它可能会在while(true)
之后和int choice=userChoice.nextInt()之前出现那正是我想要的。我以前试过,但没用。我想我一定是把主菜单选项放在int选项之后了。谢谢!