2013-10-03 97 views
0

無法弄清楚我在做java猜謎遊戲程序時做錯了什麼。計算機從1到100之間選擇一個數字,並要求用戶猜測它。用戶被提示的太低或太高,並要求再次猜測,直到他們正確。我的問題是,當你猜數字的時候,它總是會說得太低,但是如果你再次輸入相同的數字,它會說正確的。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 

     guesses = 0; 

     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 

     System.out.println("test " +housePick); 
     //Test: tells user the correct answer 
     do 
     { 
      guess = input.nextInt(); 

      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" 

        guesses = guesses+ 1 ; 



       } 

       else    //if guess is not close and guess>housePick... 
       { 
        System.out.println ("Too high, try again."); 
        //then print "Too high, Try again" 

        guesses = guesses+ 1; 


       }       
      } 
      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" 

       guesses = guesses + 1; 

      } 
      else//If guess isnt close to housePick and is less than housePick... 
      { 
       guesses = guesses+ 1; 

       System.out.println ("Too low.");//then print "too low" 
      } 

      } 

     }while (guess != housePick); //while guess doesnt = housePick... 

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

    guesses = guesses + 1; 

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




    } 
} 
+0

你做了什麼來調試它??? –

回答

2
else //If guess<housePick 

你錯了。在上述情況下,它相當於guess <= housePick。它必須是

else if(guess < housePick) 

此外,繼在housePick == guess所以沒有做guess = input.nextInt()的點,你可以簡單地說你贏了被執行的代碼塊。

}while (guess != housePick); //while guess doesnt = housePick... 

    // At this point, guess == housePick, why ask for input again???? 

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

    //guesses = guesses + 1; 
+0

哇。不能相信我不能指出這一點。 – user2809114

+2

@ user2809114發生這樣的事情。快樂編碼:) – TheKojuEffect

0

讓我們看看

Housepick = 87

猜測= 87

if ((guess + 10) >= housePick) //AND if guess is close to housePick 
{ 
     System.out.println ("close, but too low.") ; 
     //then print "close, but too low" 

     guesses = guesses + 1; 

} 

HMM的87 + 10比87

更大然後它打破了DO /而循環,並再次提示您

1

你也有一個額外的input.nextInt()在一段時間後:

}while (guess != housePick); //while guess doesnt = housePick... 

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

刪除它