这是我的代码,当在主菜单中输入选项时,我在尝试退出时无法退出程序。我想知道我哪里出了问题。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; }public static void main(String[] args) { int menu_option = 0; int num_values; int unsorted_array[] = null; main_menu(menu_option); if (menu_option == 5) { in.close(); return; } user_array(unsorted_array); }
1 回答
守着星空守着你
TA贡献1799条经验 获得超8个赞
删除该方法的参数main_menu()并在本地声明变量:a
public static int main_menu() {
int a;
// ... other existing code ...
return a;
}
在外面,回到 中main(),将“menu_option”设置为的返回值main_menu():
int menu_option = main_menu();
if (menu_option == 5) {
添加回答
举报
0/150
提交
取消