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

主菜单选择退出后程序无法终止,求Java代码问题排查

Hey there, let's dig into why your quit option (choice 5) isn't exiting the program as expected!

The Core Issue

Look at your main method: when you call main_menu(menu_option), you’re running the method to get user input—but you never save the returned menu choice back into the menu_option variable.

Since menu_option starts at 0 and never gets updated, the check if (menu_option == 5) will never be true. That’s why the program skips the exit logic entirely!

Quick Fix

Update your main method to assign the return value of main_menu to menu_option:

public static void main(String[] args) {
    int menu_option = 0;
    int num_values;
    int unsorted_array[] = null;
    // Assign the returned choice to menu_option here!
    menu_option = main_menu(menu_option);
    if (menu_option == 5) {
        in.close();
        return;
    }
    user_array(unsorted_array);
}

Optional (But Cleaner) Refactor

Your main_menu method accepts a parameter a but immediately overwrites it—this parameter is unnecessary. You can simplify the method to be parameterless for better readability:

public static int main_menu() {
    System.out.println(" ###### ##### ");
    System.out.println(" # # ## ##### ## # # #### ##### ##### # # # #### ");
    System.out.println(" # # # # # # # # # # # # # # ## # # # ");
    System.out.println(" # # # # # # # ##### # # # # # # # # # # ");
    System.out.println(" # # ###### # ###### # # # ##### # # # # # # ### ");
    System.out.println(" # # # # # # # # # # # # # # # # ## # # ");
    System.out.println(" ###### # # # # # ##### #### # # # # # # #### \n");
    System.out.println("Main Menu\n");
    System.out.println("Select the Sorting method you would like to use. ");
    System.out.println("(1) Bubble sort (Recommended for somewhat sorted data)");
    System.out.println("(2) Selection sort (Recommended for VERY unsorted data)");
    System.out.println("(3) Insertion sort (Recommened when data is nearly sorted)");
    System.out.println("(4) Turtle sort (Recommened for long arrays)");
    System.out.println("(5) Quit");
    
    int userChoice = 0;
    while(true) {
        try {
            userChoice = in.nextInt();
            break;
        } catch(Exception e) {
            System.out.println("Invalid Input, please try again.");
            in.next(); // Clear the invalid input from the scanner
        }
    }
    return userChoice;
}

Then call it in main like this:

menu_option = main_menu();

Quick Note

Make sure in is a properly initialized Scanner instance (either as a class-level variable or initialized inside main/main_menu)—otherwise you’ll run into compilation errors. You’re already closing the scanner on exit, which is good practice!

内容的提问来源于stack exchange,提问作者Ashwin Kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:54:21