該程序是一個遊戲,用戶必須猜測神奇號碼才能贏。我是新來的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