如何让我的程序在用户决定有意退出程序之前提示用户进行不同的选择?现在我试着用一个名为'exit'的布尔值来尝试一个do-while循环,试图说exit虽然是false,但要提示用户选择要做什么,尽管它只要求他们一次,然后在完成之后用户想要的,应用程序停止。更多'do while'循环的麻烦,Java
这里是我的代码:
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);
任何帮助,将不胜感激。
更好,'!exit' :) – 2012-02-09 01:13:25
@AndresF。同意! – 2012-02-09 01:15:02
如果你想明确而不是说'!exit'使用'false == exit'。这样,如果你发指令,编译器会告诉你,你不能分配给'false'。 'null'比较也很方便。 – 2012-02-09 01:16:22