简体   繁体   中英

More Trouble with 'do while' loops, Java

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.

this be your problem:

while (exit = false);

Look very carefully...

You are checking a logIn variable but the correct one should be options . Change:

while (logIn < 1 || logIn > 4)

to:

while (options < 1 || options > 4)

In addition, the while (exit = false) (attempted assignment) must be while (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 = true immediately before calling System.exit() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM