2015-10-29 137 views
0

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

而(stillplaying)是細別t需要==真正的你不需要另一個while循環... –

+0

你爲什麼使用'double'來存儲答案? –

+0

當userAnswer爲53時仍然播放爲false,並且可以刪除while(userAnswer!= 53)。 – PK20

回答

2

inner while循環不僅不是必需的,而且它也破壞了你的程序。兩種情況下,userAnswer53或不。讓我們來檢查這兩種情況。不等於53會觸發while循環。如果它小於,等於(它永遠不可能),或者大於53它會中斷。這使得循環過時。

等於53根本不會觸發循環,這是您現在遇到的結果。

import javax.swing.JOptionPane; 

class NumberFrom1To100 
{ 
    public static void main(String[] args) { 

     boolean stillplaying = true; 
     double answer = 53; 
     double userAnswer; 
     String userInput; 

     while (stillplaying) { 
      userInput = JOptionPane.showInputDialog("Guess a number between 1 and 100."); 
      userAnswer = Double.parseDouble(userInput); 

      if (userAnswer < 53) { 
       JOptionPane.showMessageDialog(null, "You picked a number LESS than the mystery number. Try again."); 
      } else if (userAnswer > 53) { 
       JOptionPane.showMessageDialog(null, "You picked a number GREATER than the mystery number. Try again."); 
      } else { 
       JOptionPane.showMessageDialog(null, "Congratulations! You just won 5 brownie points!"); 
      } 
     } 

     // System.exit(0); 
    } 
} 

我不知道你想怎麼處理「勝利」,你可以在比較設置stillplayingfalse

我還採取了刪除您的比較012iber在外部while循環的可用性,因爲它什麼都不做。使用lowerCamelCase =>stillPlaying來命名變量也是很常見的,它只能被稱爲playing,這是一個相當常見的boolean遊戲循環變量。

0

您的代碼

否則,如果(userAnswer == 53){ JOptionPane.showMessageDialog(NULL, 「恭喜你剛剛贏得5加分!」);

永遠不會達到的,因爲你已經把它裏面

而(userAnswer!= 53){} .....

只是刪除這個內循環的同時與它的相應的括號,它應該是好。

正確的代碼將是:

import javax.swing.JOptionPane; 

類NumberFrom1To100 { 公共靜態無效的主要(字串[] 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); 


      if (userAnswer < 53) { 
       JOptionPane.showMessageDialog(null, "You picked a number LESS than the mystery number. Try again."); 

      } else if (userAnswer > 53) { 
       JOptionPane.showMessageDialog(null, "You picked a number GREATER than the mystery number. Try again."); 

      } else if (userAnswer == 53) { 
       JOptionPane.showMessageDialog(null, "Congratulations! You just won 5 brownie points!"); 

      } 

    }`` 
    /* System.exit(0); */ 
} 

}

+0

if語句中的break是完全字面的打破應用程序。答案中也有一些格式問題。 – Emz