该程序是一个游戏,用户必须猜测神奇号码才能赢。我是新来的Java,我仍然在学习。当我输入53时,它只是问问题而不显示输出。我确信这只是一个简单的错误,我只是没有抓住。谢谢!while loop not printing message
import javax.swing.JOptionPane;
class NumberFrom1To100 {
public static void main(String[] args) {
boolean stillplaying = true;
double answer = 53;
double userAnswer;
String userInput;
while (stillplaying == true) {
userInput = JOptionPane.showInputDialog("Guess a number between 1 and 100.");
userAnswer = Double.parseDouble(userInput);
while (userAnswer != 53) {
if (userAnswer < 53) {
JOptionPane.showMessageDialog(null, "You picked a number LESS than the mystery number. Try again.");
break;
} else if (userAnswer > 53) {
JOptionPane.showMessageDialog(null, "You picked a number GREATER than the mystery number. Try again.");
break;
} else if (userAnswer == 53) {
JOptionPane.showMessageDialog(null, "Congratulations! You just won 5 brownie points!");
break;
}
}
}
/* System.exit(0); */
}
}
而(stillplaying)是细别t需要==真正的你不需要另一个while循环... –
你为什么使用'double'来存储答案? –
当userAnswer为53时仍然播放为false,并且可以删除while(userAnswer!= 53)。 – PK20