gpt4 book ai didi

java - java 猜数字游戏

转载 作者:行者123 更新时间:2023-12-02 04:20:34 25 4
gpt4 key购买 nike

我正在编写一个java代码,要求用户猜测计算机的随机数,我已经很好了,但是,我想做的是一条与用户猜测的次数相对应的消息。

例如,如果用户在 1 次尝试中猜中了随机数,则会输出“Excellent!”如果用户猜到了 2-4 中的答案,请尝试“干得好”等等。

我想我还没有尝试过任何东西,因为我不确定将代码粘贴在哪里?
我知道必须是 ifguess == 1 then do this else if code >1<=3 then do this 等等..但是在我的代码中?它也应该在带有嵌套 if 的 while 循环中吗?

这是我更新的代码,它按照我创建它所需的确切方式运行和编译。感谢您的帮助!

public static void main(String[] args) {
Random rand = new Random();
int compNum = rand.nextInt(100);
int count = 0;
Scanner keyboard = new Scanner(System.in);
int userGuess;
boolean win = false;
while (win == false ){
System.out.print("Enter a guess between 1 and 100: ");
userGuess = keyboard.nextInt();
count++;
if(userGuess < 1 || userGuess > 100){
System.out.println("Your guess is out of range. Pick a number between 1 and 100.");
System.out.println();
}
else if (userGuess == compNum){
win = true;
System.out.println("Congratulations! Your answer was correct! ");
}
else if (userGuess < compNum){
System.out.println("Your guess was too low. Try again. ");
System.out.println();
}
else if (userGuess > compNum){
System.out.println("Your guess was too high. Try again. ");
System.out.println();
}

}
if(count == 1){
System.out.println();
System.out.println("I had chosen " + compNum + " as the target number.");
System.out.println("You guessed it in " + count + " tries.");
System.out.println("That was lucky!");
}
else if (count > 1 && count <= 4){
System.out.println();
System.out.println("I had chosen " + compNum + " as the target number.");
System.out.println("You guessed it in " + count + " tries.");
System.out.println("That was amazing!");
}
else if (count > 4 && count <= 6){
System.out.println();
System.out.println("I had chosen " + compNum + " as the target number.");
System.out.println("You guessed it in " + count + " tries.");
System.out.println("That was good.");
}
else if (count > 6 && count <= 7){
System.out.println();
System.out.println("I had chosen " + compNum + " as the target number.");
System.out.println("You guessed it in " + count + " tries.");
System.out.println("That was OK.");
}
else if (count > 7 && count <= 9){
System.out.println();
System.out.println("I had chosen " + compNum + " as the target number.");
System.out.println("You guessed it in " + count + " tries.");
System.out.println("That was not very good.");
}
else if (count > 10){
System.out.println();
System.out.println("I had chosen " + compNum + " as the target number.");
System.out.println("You guessed it in " + count + " tries.");
System.out.println("This just isn't your game.");

}
}
}

最佳答案

您可以计算迭代次数。类似的东西。

boolean notCorrect = true;
int guesses = 0;
while(notCorrect){
//Code for checking user input.
//break out if true
guesses++;
}

抱歉,搞反了。

其他地方

if(guesses < 2) {
// display message
}
// include other if's to determine which message to display.

您可以将决定显示哪条消息的 if 语句放在检查猜测是否正确的 if 语句中。但把它从循环中拉出来。这样,如果用户确实猜对了,您就只能运行该代码。

if (userGuess == compNum){
win = true;
System.out.println("Congratulations! Your answer was correct! ");
// put message decision here...
}

关于java - java 猜数字游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32814766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com