How to keep switch statement continuing in Java

后端 未结 4 648
粉色の甜心
粉色の甜心 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:52

    One issue in your code is that method menu returns an option, So it can either be used to read only an option from the console, or to do the whole dialog, until the user decides to quit. The latter is better IMHO because it keeps the display of the menu close to the switch statemenmt:

    private static void menu()
    {
        Scanner scanner = new Scanner(System.in);
    
        System.out.println("Choose an Option");
        System.out.println("1 - FIND");
        System.out.println("2 - IN-SHUFFLE");
        System.out.println("3 - OUT-SHUFFLE");
        System.out.println("4 - QUIT");
    
        boolean quit = false;
        do {
            System.out.println("Choose an Option");
            int choice = scanner.nextInt();
    
            switch (choice) {
                case 1:
                    System.out.println("\n1 - FIND\n");
                    Deck.findTop();
                    break;
                case 2:
                    System.out.println("\n2 - IN-SHUFFLE\n");
                    // call method
                    break;
                case 3:
                    System.out.println("\n3 - OUT-SHUFFLE\n");
                    // call method
                    break;
                case 4:
                    System.out.println("\n4 - QUIT");
                    quit = true;
                    break;
                default:
                    System.out.println("\nInvalid Option");
                    break;
            }
        }
        while (!quit);
    }
    

提交回复
热议问题