0

无法弄清楚我在我的 java 猜谜游戏程序中做错了什么。计算机选择一个介于 1 和 100 之间的数字,并要求用户猜测它。提示用户太低或太高,并要求再次猜测,直到他们猜对为止。我的问题是,当您猜对数字时,它总是会说太低,但是如果您再次输入相同的数字,它会说正确。

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




    }
}
4

3 回答 3

2
else  //If guess<housePick

您对上述条件有误,它相当于guess <= housePick. 肯定是

else if( guess < housePick)

housePick == guess此外,在没有意义的情况下执行以下代码块guess = input.nextInt(),您可以简单地说You Win

}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;
于 2013-10-03T01:26:55.163 回答
1

一段时间后,您还有一个额外的 input.nextInt() :

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

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

删除它

于 2013-10-03T01:49:33.143 回答
0

让我们来看看

精选 = 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;

}

嗯 87 + 10 大于 87

然后它会中断 do/while 循环并再次提示您

于 2013-10-03T01:27:40.453 回答