我正在尝试创建一个猜谜游戏程序。用户输入一个号码并被告知该号码是否过高或过低,然后被告知再次猜测。我做了一个无限循环,我无法弄清楚如何改变它。我意识到如果猜测错误,那么程序将继续检查错误的值并打印“错误号码”消息。
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)"
}
}
答案 0 :(得分:1)
您永远不会获得用户选择并从while循环中更改guess变量的值。如果猜测永远不会改变,那么循环永远不会结束,因为它永远不会改变:while (guess != housePick)
并且条件仍然是假的。
解决方案:
显而易见:使用输入的Scanner变量从内部 while循环中获取用户输入,并使用它将猜测重新设置为新值。
答案 1 :(得分: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();
}
答案 2 :(得分: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.
答案 3 :(得分:0)
正如其他人所说,你遇到问题的原因在于你没有循环输入,只是检查。将input.nextInt()
放入while循环将解决此问题。
此外,作为一个程序点,这是使用do / while循环而不是正常循环的适当位置(因为您总是希望至少运行一次输入)。