简体   繁体   English

我希望重复我的菜单,直到我决定退出

[英]I want my menu to be repeated until I decide to exit

For now, my code receives an input, and executes the code that matches the number(user input), and terminates automatically.现在,我的代码接收输入,并执行与数字(用户输入)匹配的代码,并自动终止。 However, I want my menu to keep showing up until the user decides to exit(number 8 in my case).但是,我希望我的菜单一直显示,直到用户决定退出(在我的情况下为 8 号)。

Given the following code:给定以下代码:

    int[] main_array; 
    main_array = new int[500];

    boolean choice = false;

    int menu = 0;

    Random randomvar = new Random(); //creating random class object


    for(int i = 0; i < 500; i++){

        main_array[i] = randomvar.nextInt(1000) + 1;

    }

    while(!choice){
        while(true){
            Scanner sc = new Scanner(System.in);
            System.out.println( "Enter 1: Output all values\n"+
                                "Enter 2: Output the sum and mean of the values\n"+
                                "Enter 3: Output all odd numbers\n"+
                                "Enter 4: Output all even numbers\n"+
                                "Enter 5: Output Middle Value\n"+
                                "Enter 6: Output First value\n"+
                                "Enter 7: Output Last value\n"+
                                "Enter 8: Exit");
            try{
                menu = sc.nextInt();
                break;
            } catch(Exception e) {
                System.out.println("Wrong input");
            }
        }
        switch(menu){
            case 1:
                choice = true;
                
                for(int i = 0; i < 500; i++){

                    System.out.print(main_array[i] + " ");
                }
                break;

            case 2:
                choice = true;

                int sum = 0;

                for(int i = 0; i < 500; i++){

                    sum += main_array[i];

                }
                System.out.println("sum: " + sum);

                System.out.println("Mean average: " + (sum / 500));

                break;

            case 3:
                choice = true;

                for(int i = 0; i < 500; i++){

                    if(main_array[i] % 2 == 1)

                        System.out.print(main_array[i] + " ");
                }
                break;

            case 4:
                choice = true;

                for(int i = 0; i < 500; i++){

                    if(main_array[i] % 2 == 0)

                        System.out.print(main_array[i] + " ");
                }
                break;

            case 5:
                choice = true;

                System.out.println(main_array[500/2]);

                System.out.println(main_array[500/2]-1);
                break;

            case 6:
                choice = true;

                System.out.println(main_array[0]);

                break;

            case 7:
                choice = true;

                System.out.println(main_array[500 - 1]);

                break;

            case 8:
                System.out.println("Exit the program");

                return;

            default:
                System.out.println("Invalid input!");

                break;

        }
    }
}

I want my menu to be keep repeated until I input the number 8, so I tried to edit my code by replacing the switch-statement inside the while(true) loop, but the output shows like this:我希望我的菜单一直重复,直到我输入数字 8,所以我尝试通过替换 while(true) 循环内的 switch 语句来编辑我的代码,但是 output 显示如下:

Enter 1: Output all values
Enter 2: Output the sum and mean of the values
Enter 3: Output all odd numbers
Enter 4: Output all even numbers
Enter 5: Output Middle Value
Enter 6: Output First value
Enter 7: Output Last value
Enter 8: Exit

2

Enter 1: Output all values
Enter 2: Output the sum and mean of the values
Enter 3: Output all odd numbers
Enter 4: Output all even numbers
Enter 5: Output Middle Value
Enter 6: Output First value
Enter 7: Output Last value
Enter 8: Exit

5

Enter 1: Output all values
Enter 2: Output the sum and mean of the values
Enter 3: Output all odd numbers
Enter 4: Output all even numbers
Enter 5: Output Middle Value
Enter 6: Output First value
Enter 7: Output Last value
Enter 8: Exit

Fixed code:固定代码:

    int[] main_array; 
    main_array = new int[500];

    boolean choice = false;

    int menu = 0;

    Random randomvar = new Random(); //creating random class object


    for(int i = 0; i < 500; i++){

        main_array[i] = randomvar.nextInt(1000) + 1;

    }

    while(!choice){
        while(true){
            Scanner sc = new Scanner(System.in);
            System.out.println( "Enter 1: Output all values\n"+
                                "Enter 2: Output the sum and mean of the values\n"+
                                "Enter 3: Output all odd numbers\n"+
                                "Enter 4: Output all even numbers\n"+
                                "Enter 5: Output Middle Value\n"+
                                "Enter 6: Output First value\n"+
                                "Enter 7: Output Last value\n"+
                                "Enter 8: Exit");
            try{
                menu = sc.nextInt();
                break;
            } catch(Exception e) {
                System.out.println("Wrong input");
            }
            switch(menu){
                case 1:
                    choice = true;
                    
                    for(int i = 0; i < 500; i++){

                        System.out.print(main_array[i] + " ");
                    }
                    break;

                case 2:
                    choice = true;

                    int sum = 0;

                    for(int i = 0; i < 500; i++){

                        sum += main_array[i];

                    }
                    System.out.println("sum: " + sum);

                    System.out.println("Mean average: " + (sum / 500));

                    break;

                case 3:
                    choice = true;

                    for(int i = 0; i < 500; i++){

                        if(main_array[i] % 2 == 1)

                            System.out.print(main_array[i] + " ");
                    }
                    break;

                case 4:
                    choice = true;

                    for(int i = 0; i < 500; i++){

                        if(main_array[i] % 2 == 0)

                            System.out.print(main_array[i] + " ");
                    }
                    break;

                case 5:
                    choice = true;

                    System.out.println(main_array[500/2]);

                    System.out.println(main_array[500/2]-1);
                    break;

                case 6:
                    choice = true;

                    System.out.println(main_array[0]);

                    break;

                case 7:
                    choice = true;

                    System.out.println(main_array[500 - 1]);

                    break;

                case 8:
                    System.out.println("Exit the program");

                    return;

                default:
                    System.out.println("Invalid input!");

                    break;
        }
        }
    }

How should I fix it?我应该如何解决它?

You can remove the while(true) loop and instead of setting choice=true in all the cases just set it to true in the case 8 .您可以删除while(true)循环,而不是在所有情况下设置choice=true ,只需在case 8中将其设置为 true 。

You can also use do while loop and add condition in while like choice is equal to exit.您还可以使用 do while 循环并在 while 中添加条件,例如选择等于退出。

int choice = -1;
do{

choice = scanner.nextInt();
// your logic

}while(choice != 8);

As said above, you have two loops.如上所述,您有两个循环。

And you create a new Scanner object in your loop, which is really messy.你在你的循环中创建了一个新的 Scanner object,这真的很混乱。

Why not creating a menu function, for example:为什么不创建一个菜单 function,例如:

private static void menu() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("menu");
            switch (scanner.nextInt()) {
                case 1:
                    // foo
                    break;
                // and so on until case 8 :
                case 8:
                    scanner.close();
                    return; // Leave your function. You don't even need to use a boolean
            }
        }
    }

Note that if you need to use the scanner outside of the function, declare it before and pass it as a parameter of the function.请注意,如果您需要在 function 之外使用扫描仪,请在之前声明并作为 function 的参数传递。 (You should have only one instance of scanner) (您应该只有一个扫描仪实例)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我希望我的计算器重新启动,直到我按下“q”字母 - I want my calculator to restart until I press the 'q' letter 我想在菜单中添加图片 - I want to add a picture to an item in my menu 我如何将我的应用程序退出到主屏幕并添加,确定要退出按钮 - how i can exit my app to home screen and add are you sure want to exit button 我需要帮助 我想退出程序 - I need help I want to exit the program 如何动态决定我想要多少个循环? - How to dynamically decide how many loops I want? 我有两个 Java 对象,我想决定在运行时要返回哪个对象 - I have two Java objects and I want to decide which i want to return at runtime 需要帮助创建一个循环,允许用户与菜单交互,直到他们发出想要退出系统的信号。 (爪哇) - Need help creating a loop that allows the user to interact with the menu until they signal they want to exit the system. (Java) 我想刷新浏览器,直到出现登录页面 - JAVA -Selenium - I want to refresh my browser until my login page appears - JAVA -Selenium (CMD)我想在退出后做点事 - (CMD) i want to do something after exit 如何保持我的程序运行,直到在菜单上按下退出键? -图书馆应用 - How to keep my program running until exit is pressed on the menu? - Library Applicatiion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM