简体   繁体   中英

Looping through a switch statement

So I am writing a menu based program, and I am stuck in one part. Here's my code:

public static void main(String [] args) throws FileNotFoundException {
    switch (menu()) {
        case 1:
            System.out.println("Stub 1");
            menu();
            break;
        case 2:
            System.out.println("Stub 2");
            menu();
            break;
        case 3:
            System.out.println("Stub 3");
            menu();
            break;
        case 4:
            System.out.println("Program Terminated");
            break;

    }

}

public static int menu() {
    System.out.println("Choose a task number from the following: ");
    System.out.println("\t1. - See histogram of name's popularity");
    System.out.println("\t2. - Compare two names in a specific decade");
    System.out.println("\t3. - Show what name had a specific rank for a certain decade");
    System.out.println("\t4. - Exit program");
    int opt = 0;
    int option = getInt(input,"Enter number (1-4): ", 1, 4);
    if (option == 1) {
        opt = 1;
    }
    else if (option == 2) {
        opt = 2;
    }
    else if (option == 3) {
        opt = 3;
    } 
    else {
        opt = 4;
    }
    return opt;
}

My question is, how can I get the menu to 'reset' after an option is pressed. For example, I choose 1, the program performs the action and after it's done, it shows the options menu again until I press 4 to terminate it.

The getInt method in my code simply returns an int between 1 and 4.

One easy option is to declare a Boolean variable and wrap the switch in a while loop eg

Boolean quit = false;
while (!quit)        //or do-while
{
    int opt = menu();
    switch(opt)
    {
        //other cases...
        case 4:
            quit = true;
    }
}

I'm not sure why you're calling menu in each case though.

我不是用Java编写代码,而是尝试在每种情况的结尾将其指向默认情况,以便在程序完成操作后将默认返回菜单。

For my menus I always wrap the menu options and request in a do-while loop.

do{
menu code...
} while (menu() != 4);

You can keep the code in an infinite loop and exit the program when 4 is pressed. It is not required to call menu() in all your cases because you have to display the menu only one in every iteration.

For making an infinite loop use

while(true) {
  //some code
}

for exiting the program use:

System.exit(0);

Try this :

while(true) {
     int choice = menu();
     switch (choice) {
        case 1:
            System.out.println("Stub 1");

            break;
        case 2:
            System.out.println("Stub 2");

            break;
        case 3:
            System.out.println("Stub 3");

            break;
        case 4:
            System.out.println("Program Terminated");
            System.exit(0); // for terminating the program

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM