2013-10-02 219 views
1

我想創建一個猜謎遊戲程序。用戶輸入一個數字,並被告知該數字是否過高或過低,然後被告知再次猜測。我做了一個無限循環,我無法弄清楚如何改變它。我意識到,如果猜測是錯誤的,那麼程序將繼續檢查錯誤的值並打印「錯誤的數字」消息。無限預測試while循環(java)

package guessinggame; 
import java.util.Scanner; 
/** 
* 
* @author 
*/ 
public class GuessingGame { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) 
    { 
     Scanner input = new Scanner (System.in); 

     int guesses; //number of users guesses 

     int housePick; //number the user must guess 

     int guess;  //users guess 

     housePick = (int)((Math.random() * 100) +1); 
     //sets housePick to random number from 1 to 100 

     System.out.println("I'm thinking of a number between 1 and 100") ; 
     //print "Im thinking of a nubmer between 1 and 100" 

     System.out.println("Can you guess what it is?"); 
     //print "can you guess what it is" 

     System.out.println 
       ("Enter a number from 1 to 100 (including 1 and 100)"); 
     //prompt user to enter number 

     guess = input.nextInt(); 
     //save entered number as guess 

     while (guess != housePick) //while guess doesnt = housePick... 
     { 
      if (guess > housePick) //and if guess > housePick... 
      { 
       if ((guess - 10) <= housePick) 
        //and if guess is 10 numbers away from housePick... 

       { 
        System.out.println("Close, but too high. Try again."); 
        //print "close but too high, try again" 

       } 
       else    //if guess is not close and guess>housePick... 
       { 
        System.out.println ("Too high, try again."); 
        //then print "Too high, Try again" 
       }       
      } 
      else //If guess<housePick 
      { 
      if ((guess + 10) >= housePick) //AND if guess is close to housePick 
      { 
       System.out.println ("close, but too low.") ; 
       //then print "close, but too low" 

      } 
      else//If guess isnt close to housePick and is less than housePick... 
      { 
       System.out.println ("Too low.");//then print "too low" 
      } 

      } 

     } 



     System.out.println ("You win! It took you " + "guesses."); 
     //If guess = housePick print "Yout win! It took you (# of guesses)" 




    } 
} 

回答

1

您永遠不會獲得用戶選擇並在while循環內更​​改guess變量的值。如果猜測永遠不會改變,循環永遠不會結束,因爲這永遠不會改變:while (guess != housePick)並且條件保持爲假。

解決方案:
做明顯的:用你的輸入掃描儀變量從獲取用戶輸入的內部 while循環,並用它來猜測重新設置爲一個新值。

1

你是正確的達到某個水平,但當出現問題時,你必須在循環結束之前從用戶那裏獲得輸入。所以,你必須得到玩家的輸入,while循環ends.I也做了修正和更新的代碼(只有while循環的一部分)之前如下

while (guess != housePick) //while guess doesnt = housePick... 
    { 
     if (guess > housePick) //and if guess > housePick... 
     { 
      if ((guess - 10) <= housePick) 
       //and if guess is 10 numbers away from housePick... 

      { 
       System.out.println("Close, but too high. Try again."); 
       //print "close but too high, try again" 

      } 
      else    //if guess is not close and guess>housePick... 
      { 
       System.out.println ("Too high, try again."); 
       //then print "Too high, Try again" 
      }       
     } 
     else //If guess<housePick 
     { 
     if ((guess + 10) >= housePick) //AND if guess is close to housePick 
     { 
      System.out.println ("close, but too low.") ; 
      //then print "close, but too low" 

     } 
     else//If guess isnt close to housePick and is less than housePick... 
     { 
      System.out.println ("Too low.");//then print "too low" 
     } 

     } 
      /// this is the correction 
      System.out.println 
      ("Enter a number from 1 to 100 again (including 1 and 100)"); 
      //prompt user to enter number 

      guess = input.nextInt(); 
    } 
0

添加以下行只是while循環之前結束,以便每當猜測錯誤時都會詢問,並在猜測正確時成功退出循環。

while (guess != housePick) //while guess doesnt = housePick... 
    { 
     if (guess > housePick) 
     { 
      \\Your code remains as it is. 
       ... 
       ... 
       ... 
       ... 
     } //if-else loop ends here. 
     guess = input.nextInt(); 
    }//while ends here. 
0

正如別人所說,你有問題的原因源於你沒有循環輸入,只檢查。將您的input.nextInt()放入while循環將解決此問題。

另外,作爲過程的一個步驟,這是使用do/while循環而不是普通的適當位置(因爲您總是希望至少運行一次輸入)。