I\'m looking to keep the following menu repeating:
Choose an Option
1 - FIND
2 - IN-SHUFFLE
3 - OUT-SHUFFLE
int choice = scanner.nextInt();
) is only asked once before the loop. So the choice
is fix before entering the loop.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.boolean quit
is never changed inside your loop, so exit condition of your loop quit == true
will never be reached.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).
quit = true
inside an additional case 4: quit = true; break;
int choice = scanner.nextInt();
inside do {}
and directly before switch
.4 - QUIT
), and use this option (see 1)Design the loop first: focus on exit!
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! 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.