该应用程序是一个数字猜谜游戏我的问题是当用户得到正确的随机数时,如果他输入“y”它会询问用户是否要继续它会创建一个新的随机数并要求用户猜测它。然后它应该要求用户“输入一个数字”。相反,我得到
“太高”(或“太低”)
“输入数字”
ex 输出:
输入数字:
2
正确,你知道了!数字是 2 你在 2 次尝试中得到它。
你想再玩一次吗 (y/n):
y
太低了!再试一次。
输入号码:
我如何在不打印假设在他输入数字后确定的“太高”或“太低”的情况下解决问题?
附言。我尝试了很多方法,但被卡住了:(
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.");
}
}
}
}