How to keep switch statement continuing in Java

后端 未结 4 605
粉色の甜心
粉色の甜心 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条回答
  •  -上瘾入骨i
    2021-01-28 14:01

    What happens?

    1. User's choice (int choice = scanner.nextInt();) is only asked once before the loop. So the choice is fix before entering the loop.
    2. Your do {} while loops only the switch-statement which executes some code, based on the user's choice (actually prints single fixed choice). The user has no chance to change this inside the loop.
    3. The loop's control-variable boolean quit is never changed inside your loop, so exit condition of your loop quit == true will never be reached.
    4. the menu you're showing the user does not contain any option to quit 4 - QUIT

    Summary: Once user enters a choice (1), the program gets stuck in a so called infinite-loop (3) always printing the current choice (2). Even after a restart the uninformed user has no chance to exit, since neither the menu does list this option (4), nor would choice = 4 have an effect since it's not in switch's cases (3).

    Resolving Issues

    1. Modify the loop's control-variable inside the loop: set quit = true inside an additional case 4: quit = true; break;
    2. Make sure this only exiting branch will accessed by reading user's choice inside the loop: put int choice = scanner.nextInt(); inside do {} and directly before switch.
    3. Add an option to your menu so user knows how to exit (e.g. 4 - QUIT), and use this option (see 1)
    4. Show the menu inside the loop, directly before reading the user's choice.

    Recommendations

    Design the loop first: focus on exit!

    1. Use while(exitCondition == false) {} loops instead of do {} while(exitConfition == false) because very important exitCondition is shown first (to every reader of your code), and it's controlling and avoiding an infinite-loop. So, rememeber setting it inside the loop!
    2. Keep the code-lines inside your loop short, and modify control-variables on top, not inside nested blocks.
    3. When designing loops (especially for user-interaction), do it first, and use pseudo-code (see lines commented out below).
        final static int QUIT_OPTION = 4;
    
        public static void main(String[] args) {
            int chosenOption = -1;
            while (chosenOption != QUIT_OPTION) {
                // showMenu();
                // chosenOption = readUsersChoice();   
                // executeOption(chosenOption);
                System.out.println("choosenOption: " = choosenOption); // for testing: see if choosenOption does change
                choosenOption = QUIT_OPTION; // while design: simulates a single loop
            }
        }
    

    The last 2 lines inside the loop are only for design-phase, where you will test a single loop (hence the final activating of QUIT_OPTION). Put your focus on the control-variable: Is your user or program able to change it in desired way (hence the debugging println)?

    Divide and conquer: split task/logic into small separate functions The loop above (inside main) was the first small but main part of logic.

    First task inside the loop: "displaying a menu"

        private static void showMenu() {
            System.out.println("Options - Main Menu");
            System.out.println("1 - FIND");
            System.out.println("2 - IN-SHUFFLE");
            System.out.println("3 - OUT-SHUFFLE");
            System.out.println("4 - QUIT");
            System.out.println("Choose from above Options (1-4) by typing the number followed by RETURN:");
        }
    

    Try to keep this methods small, focused on one task. If possible keep them also isolated (from others). So you can test (call) them separately and check if the do what you want.

    Next: "read the user's choice"

        private static int readUsersChoice() {
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
    
            return choice;
        }
    

    This method needs special testing. Try what's happening if user enters a letter (e.g. Q because wishing to quit). Did you catch the java.util.InputMismatchException inside the method?

    Finally the "choosen option will be evaluated and executed":

        private static void executeOption(int choice) {
            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;
                default:
                    System.out.println("\nInvalid Option");
                    break;
            }
        }
    

    Test it well, since here -based on an option- some other code/methods (even loops) could be called. You can also add the case: QUIT_OPTION and print a Good-Bye.

提交回复
热议问题