How do I get my program to prompt the user for different choices until they decide to purposefully exit the program? Right now I tried a do-while loop with a boolean called ‘exit,’ trying to say that while exit is false keep prompting the user to choose what to do, though all it does is ask them once and after it has done what the user wanted, the application stops.
Here is my code:
boolean exit = false;
do {
int options = 0;
do {
options = Integer.parseInt(JOptionPane.showInputDialog(null,
"Would you like to:"
+ "\n(Enter number value of option you would like to choose.)\n"
+ "\n1. See your recommendations. \n2. See top rated books."
+ "\n3. See random books of the day. \n4. Exit"));
} while (logIn < 1 || logIn > 4);
if (options == 1) {
recommend.displayRecommendations(custIndex);
} else if (options == 2) {
calculations.displayTopTen();
} else if (options == 3) {
calculations.displayRandomBooks();
} else if (options == 4) {
exit = true;
System.exit(0);
}
} while (exit = false);
Any help would be appreciated.
You are checking a
logInvariable but the correct one should beoptions. Change:to:
In addition, the
while (exit = false)(attempted assignment) must bewhile (exit == false)(comparison) or, even better,while (!exit)And, lastly, though it doesn’t matter that much, it’s a bit superfluous to set
exit = trueimmediately before callingSystem.exit().