在情况6中,我需要学习如何返回主菜单,它可以确认退出,但是我不确定如何循环回到菜单

时间:2018-10-18 16:41:35

标签: java

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

1 个答案:

答案 0 :(得分:0)

您的同时对其进行评估:

while (choice != 6){}

在第6种情况下进行切换后,while将不再执行。

如果用户决定不退出程序,则可以更改选择的值。

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