[英]How can I return to the main menu?
我有一个项目,如果它是从第二个菜单中选择的,我希望它返回到第一个菜单。
以下只是一个例子:
import java.util.Scanner;
public class SAMPLE {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
这是第一个菜单:
System.out.println("Enter choice: ");
System.out.println("1 = A");
System.out.println("2 = B");
int choice1 = input.nextInt();
do {
这是第二个菜单:
System.out.println("Another choice:");
System.out.println("1 = C");
System.out.println("2 = D");
System.out.println("3 = Go back");
int choice2 = input.nextInt();
switch (choice2) {
case 1: System.out.println("nice");
break;
case 2: System.out.println("nicee");
break;
如果我选择了 3,它应该回到第一个菜单,但我不知道如何。
case 3: System.out.println("How can I go back to the first menu? HELP!");
break;
}
} while (choice1 > 2);
System.out.println("END OF THE PROGRAM");
}
}
我建议将您的代码拆分为函数。
在情况 3 - 你应该调用你的新函数。
像这样的东西:
static int mainMenu(Scanner inputScanner)
{
System.out.println("Enter choice: ");
System.out.println("1 = A");
System.out.println("2 = B");
return inputScanner.nextInt(); //Assume valid input. you should add a valid check
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int Choice1 = mainMenu(input);
do {
System.out.println("Another choice:");
System.out.println("1 = C");
System.out.println("2 = D");
System.out.println("3 = Go back");
int Choice2 = input.nextInt();
switch (Choice2) {
case 1: //DO Y
break;
case 2: //DO X
break;
case 3: Choice1 = mainMenu(input);
break;
}
} while (Choice1 > 2);
//END logic
}
啊哈在选择C和D的过程中你想回到选择A和B的第一个菜单!
有很多方法(例如使用另一种方法或递归),但我推荐这种方法,它现在更容易理解。
首先,让我们检查代码以组织我们想要做的事情<我稍微更改了它以使其更易于识别! 抱歉! >
import java.util.Scanner;
public class StackOver {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
////main_menu///-Select A or B
System.out.println("Please input Enter Choice: ");
System.out.println("input '1' to Select A");
System.out.println("input '2' to Select B");
int choice1 = input.nextInt();
////////////////////////////
////second_Menu Choose C or D, or Back to main_menu
System.out.println("Another choice: ");
System.out.println("input '1' to Select C");
System.out.println("input '2' to Select D");
System.out.println("input '3' to Go back");
int choice2 = input.nextInt();
//According to 'choice2'
switch(choice2) {
case 1: System.out.println("nice, you Select C");
break;
case 2: System.out.println("nicee, you Select D");
break;
case 3: System.out.println("Now, you can go to Back");
break;
}
//I'm sorry, but I can't understand the meaning of
//'while (choice1 > 2);' ㅠㅅㅠ
System.out.println("END OF THE PROGRAM");
}
}
所以上面的代码就是你要的,对吧?
有很多方法可以按照您想要的方向实现它。
让我们尝试通过您尝试使用的 While 语句将其返回到主菜单。
代码进展。
它包括转到 (1) 或结束程序,具体取决于 (2) 的结果。
如何使用 do{ }while(boolean);?? do{} 中的过程将继续,直到 'boolean' 为假。
现在我们可以通过这种方式更改代码
import java.util.Scanner;
public class StackOver {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int choice2 = 0; //init for NullPointerError
do {
//below do{
////main_menu///-Select A or B
System.out.println("Please input Enter Choice: ");
System.out.println("input '1' to Select A");
System.out.println("input '2' to Select B");
int choice1 = input.nextInt();
////////////////////////////
////second_Menu Choose C or D, or Back to main_menu
System.out.println("Another choice: ");
System.out.println("input '1' to Select C");
System.out.println("input '2' to Select D");
System.out.println("input '3' to Go back");
choice2 = input.nextInt();
//According to 'choice2'
switch(choice2) {
case 1: System.out.println("nice, you Select C");
break;
case 2: System.out.println("nicee, you Select D");
break;
case 3: System.out.println("Now, you can go to Back");
break;
}
}while(choice2 == 3);
//if choice2 == 3, then the progress of code return back to below 'do{'
System.out.println("END OF THE PROGRAM");
}
}
我希望你能理解它,如果有什么难以理解的,请发表评论! 我希望你今天有一个愉快和平静的一天。😻😻
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.