Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用if语句时无法退出程序_Java_Arrays_If Statement_Exit - Fatal编程技术网

Java 使用if语句时无法退出程序

Java 使用if语句时无法退出程序,java,arrays,if-statement,exit,Java,Arrays,If Statement,Exit,这是我的代码,当在主菜单中输入选项时,我无法在尝试退出程序时退出。我想知道我哪里出错了 public static int main_menu(int a) { System.out.println(" ###### ##### "); System.out.println(" # # ## ##### ## #

这是我的代码,当在主菜单中输入选项时,我无法在尝试退出程序时退出。我想知道我哪里出错了

public static int main_menu(int a) {
        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");

        while(true) {
            try {
                a = in.nextInt();
                break;
                }
            catch(Exception e) {
                System.out.println("Invalid Input, please try again.");
                in.next();
                }
            }
        return a;
        }

去掉
main_menu()
的参数,并在该方法中本地声明
a
变量:

public static int main_menu() {
    int a;
    // ... other existing code ...
    return a;
}
在外部,回到
main()
,将“菜单选项”设置为
main\u菜单()的返回值:


当您调用一个方法并希望它向您返回一些东西时,将该响应存储在某个地方是非常有用的。您的代码忽略了由
主菜单返回的信息。我认为它是由主方法中的变量菜单选项接收的。不,不是。看:我至少可以知道正确的方法是什么吗?
public static int main_menu() {
    int a;
    // ... other existing code ...
    return a;
}
    int menu_option = main_menu();
    if (menu_option == 5) {