该应用程序是一个数字猜谜游戏,我的问题是当用户获得正确的随机数时,它询问用户是否要继续,如果他键入"y"它会创建一个新的随机数并要求用户猜测它.然后它应该询问用户"输入一个数字".相反,我得到了
"太高"(或"太低")
"输入数字"
输出:
输入数字:
2
正确你得到它!数字是2你在2次尝试得到它.
你想再次发挥(Y/N):
Ÿ
太低了!再试一次.
输入数字:
我如何得到它而不是打印"太高"或"太低"的问题,这是在他输入一个数字后确定的?
PS.我尝试了很多方法,但卡住了:(
public static void main(String[] args) {
System.out
.println("Welcome to the gussing game, Try to guess the number am thinking of to win!");
System.out
.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println();
System.out
.println("Am thinking of a number between 0 and 10. Try to guess it.");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
double rightNum = Math.random() * 10;
int randomNum = (int) rightNum; // convert the random number to int
int tries = 0;
while (!choice.equalsIgnoreCase("n")) {
System.out.println("Enter the Number:");
int guess = sc.nextInt();
tries++;
if (guess == randomNum) {
System.out.println("Correct you've got it! The Number Was "
+ randomNum);
System.out.println("You got it in " + tries + " tries.");
System.out.println("Would you like to play again (y/n):");
choice = sc.next();
if (choice.equalsIgnoreCase("y"))
// reset the random number
{
rightNum = Math.random() * 10;
randomNum = (int) rightNum;
tries = 0;
}
}
if (guess > randomNum + 10) {
System.out.println("Way to high! Try again.");
} else if (guess < randomNum) {
System.out.println("Too low! Try again.");
} else if (guess > randomNum && guess <= randomNum + 10) {
System.out.println("Too high! Try again.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
else if (guess > randomNum + 10)
{
System.out.println("Way to high! Try again.");
}
Run Code Online (Sandbox Code Playgroud)
你错过了其他的.