1

我正在尝试创建一个猜谜游戏程序。用户输入一个数字并被告知该数字是否太高或太低,然后被告知再次猜测。我做了一个无限循环,我不知道如何改变它。我意识到如果猜错了,那么程序将继续检查错误的值并打印“错误的数字”消息。

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




    }
}
4

4 回答 4

1

您永远不会从 while 循环中获得用户选择并更改guess 变量的值。如果guess 从未改变,则循环将永远不会结束,因为它永远不会改变:while (guess != housePick)并且条件保持为假。

解决方案:做显而易见的事情:使用您的输入 Scanner 变量从 while 循环内部
获取用户输入,并使用它将guess 重新设置为新值。

于 2013-10-02T05:12:55.713 回答
1

您在某种程度上是正确的,但是当出现问题时,您必须在循环结束之前从用户那里获得输入。所以你必须在while循环结束之前从玩家那里获得输入。我已经完成了更正和更新的代码(只有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();
    }
于 2013-10-02T05:15:06.600 回答
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.
于 2013-10-02T05:17:37.787 回答
0

正如其他人所提到的,您遇到问题的原因在于您没有循环输入,只是检查。将您input.nextInt()放入 while 循环将解决此问题。

此外,作为一个过程点,这是使用 do/while 循环而不是普通 while 循环的适当位置(因为您总是希望至少运行一次输入)。

于 2013-10-02T05:18:27.457 回答