How to keep switch statement continuing in Java

后端 未结 4 677
粉色の甜心
粉色の甜心 2021-01-28 13:21

I\'m looking to keep the following menu repeating:

Choose an Option

1 - FIND

2 - IN-SHUFFLE

3 - OUT-SHUFFLE

4条回答
  •  梦谈多话
    2021-01-28 13:36

    The selected option stays the same throughout the loop. You need to set quitto true for the loop to exit. I suggest adding an option to quit to the switch statement, where you do just that.

    To be able to change the selected option, you need to ask for input inside the loop.

    Scanner sc = new Scanner(System.in);
    int choice;
    boolean quit = false;
    do {
      choice = sc.nextInt();
      switch (choice) {
        case 1:
          // case 1
          break;
        case 2:
          // case 2
          break;
        case 3:
          // case 3
          break;
        case 4:
          quit = true;
          break;
        default:
          // your default here
      }
    } while (!quit);
    

提交回复
热议问题