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) {