vendredi 18 octobre 2019

Unable to exit from the Program when using if statments

Here is my code, when inputting the options in the main menu, I am unable to exit from the program when trying to exit. I would like to know where I have gone wrong.

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);
        }

Aucun commentaire:

Enregistrer un commentaire