一旦用户输入6,他们就会看到两个选择,退出程序或返回主菜单。因为我无法使用返回选项,所以我不知道有任何其他方法可以返回菜单
public static void main(String[] args) {
byte choice = 0;
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,"This program performs various
computations." +
"\n You will be shown a menu from which" +
"\n you can choose the operations that will be
performed. " +
"\n\n Please click on OK to continue", "Introduction",
JOptionPane.INFORMATION_MESSAGE);
while (choice != 6){
choice = Byte.parseByte(JOptionPane.showInputDialog(null, "
MAIN MENU\n" +
"1. Routines for Triangles\n" +
"2. Routines for Temperatures\n" +
"3. Routines for Geometric Figures\n" +
"4. String Manipulations\n" +
"5. Miscellaneous Simple Games.\n" +
"6. Quit", "Input",JOptionPane.QUESTION_MESSAGE));
switch (choice){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
Object[] quit = {"Yes, exit the program", "No, bring me back
to the menu"};
int option = JOptionPane.showOptionDialog(null,"Are you sure
you want to exit?",
"Choice",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,
quit, quit[0]);
if (option == 0){
System.exit(0);
} //<-- needs to loop back to menu
break;
}
}
}
最佳答案
您的在评估此内容时:
while (choice != 6){}
当你在情况 6 中得到 switch 时, while 将不再执行。
如果用户决定不退出程序,您可以更改选择的值。
if(option !=0){choice=7; break;}
关于java - 我需要学习如何在情况 6 中返回主菜单,它确认退出,但我不确定它如何循环回菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52878797/