首页 > 解决方案 > 我需要学习如何在案例 6 中返回主菜单,它确认退出,但我不确定它如何循环返回菜单

问题描述

一旦用户输入 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;
            }
       }
   }

标签: java

解决方案


评估这个时:

while (choice != 6){}

当您在案例 6 中获得开关时,while 将不再执行。

如果用户决定不退出程序,您可以更改 choise 的值。

if(option !=0){choice=7; break;}

推荐阅读