无限预测试while循环(java)

无限预测试while循环(java),java,loops,infinite,Java,Loops,Infinite,我正在尝试创建一个猜谜游戏程序。用户输入一个数字,并被告知该数字是否过高或过低,然后被告知再次猜测。我做了一个无限循环,我不知道如何改变它。我意识到,如果猜测是错误的,那么程序将继续检查错误的值并打印“错误的数字”消息 package guessinggame; import java.util.Scanner; /** * * @author */ public class GuessingGame { /** * @param args the command li

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

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




    }
}
包猜游戏;
导入java.util.Scanner;
/**
*
*@作者
*/
公开课猜谜游戏{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
int猜测;//用户猜测的数量
int housePick;//用户必须猜测的数字
int guess;//用户猜测
housecik=(int)((Math.random()*100)+1);
//将housePick设置为1到100之间的随机数
System.out.println(“我想到的是一个介于1和100之间的数字”);
//打印“我想到一个介于1和100之间的数值”
System.out.println(“你能猜出它是什么吗?”);
//打印“你能猜出是什么吗”
System.out.println
(“输入1到100之间的数字(包括1和100)”;
//提示用户输入数字
guess=input.nextInt();
//将输入的数字另存为猜测
while(guess!=houseck)//while guess不=houseck。。。
{
如果(猜测>家选)//如果猜测>家选。。。
{
如果((猜测-10)家政选择。。。
{
System.out.println(“太高,请重试。”);
//然后打印“太高,再试一次”
}                         
}
else//如果guess=houseck)//如果guess接近houseck
{
System.out.println(“关闭,但过低”);
//然后打印“关闭,但过低”
}
否则//如果guess不接近Houseck,并且小于Houseck。。。
{
System.out.println(“太低”);//然后打印“太低”
}
}
}
System.out.println(“你赢了!你用了”+“猜测”);
//If guess=Houseck打印“Yout win!它花费了你(#个猜测)”
}
}

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

解决方案:

做显而易见的事情:使用输入扫描变量从while循环内部获取用户输入,并使用它将guess重新设置为一个新值。

在某种程度上你是正确的,但当事情出错时,你必须在循环结束之前从用户处获取输入。因此,您必须在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();
    }
while(guess!=housecik)//while guess doesnt=housecik。。。
{
如果(猜测>家选)//如果猜测>家选。。。
{
如果((猜测-10)家政选择。。。
{
System.out.println(“太高,请重试。”);
//然后打印“太高,再试一次”
}                         
}
else//如果guess=houseck)//如果guess接近houseck
{
System.out.println(“关闭,但过低”);
//然后打印“关闭,但过低”
}
否则//如果guess不接近Houseck,并且小于Houseck。。。
{
System.out.println(“太低”);//然后打印“太低”
}
}
///这是更正
System.out.println
(“再次输入1到100之间的数字(包括1和100)”;
//提示用户输入数字
guess=input.nextInt();
}

在循环结束时,在
之前添加下一行,以便每次猜测错误时它都会询问,猜测正确时它会成功退出循环

 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.

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

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