You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java控制台菜单循环运行至输入8退出的代码修复问题

问题排查

  • 原始代码执行一次就终止的原因:你在case 1~7的所有分支逻辑里都加了choice = true,外层循环的判断条件是while(!choice),所以执行完一次功能后外层循环就直接退出了,不会再次显示菜单。
  • 修改后代码只打印菜单不执行功能的原因:你把switch分支放在了try-catch结构之后,而try代码块中只要成功读取到合法的数字输入,就会直接执行break跳出内层while(true)循环,switch逻辑永远不会被触发,自然只有菜单打印没有功能执行。

修复代码

你只需要删除所有case 1~7里的choice = true语句即可实现菜单循环,不需要调整代码结构,同时补充了输入非法字符后的缓冲区清空逻辑,避免死循环,修正后完整代码如下:

import java.util.Random;
import java.util.Scanner;

public class MenuDemo {
    public static void main(String[] args) {
        int[] main_array; 
        main_array = new int[500];

        boolean choice = false;
        int menu = 0;
        Random randomvar = new Random();

        for(int i = 0; i < 500; i++){
            main_array[i] = randomvar.nextInt(1000) + 1;
        }
        
        // Scanner移到循环外,避免重复创建对象
        Scanner sc = new Scanner(System.in);
        while(!choice){
            while(true){
                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");
                    // 清空输入缓冲区非法内容,避免死循环
                    sc.next();
                }
            }
            switch(menu){
                case 1:
                    for(int i = 0; i < 500; i++){
                        System.out.print(main_array[i] + " ");
                    }
                    System.out.println();
                    break;

                case 2:
                    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:
                    for(int i = 0; i < 500; i++){
                        if(main_array[i] % 2 == 1)
                            System.out.print(main_array[i] + " ");
                    }
                    System.out.println();
                    break;

                case 4:
                    for(int i = 0; i < 500; i++){
                        if(main_array[i] % 2 == 0)
                            System.out.print(main_array[i] + " ");
                    }
                    System.out.println();
                    break;

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

                case 6:
                    System.out.println(main_array[0]);
                    break;

                case 7:
                    System.out.println(main_array[500 - 1]);
                    break;

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

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

内容的提问来源于stack exchange,提问作者이우진

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 16:15:03