英文:
How do I add a limit to the number of guesses in a number guessing game?
问题
我无法弄清楚如何在我的数字猜谜游戏中添加猜测次数的限制。我尝试在while语句后面添加了一个for语句,但代码只会在第十次猜测时停止,而且永远没有赢家。我删除了while语句,只使用了for语句,这确保用户在第九次猜测时得到正确的答案。我的代码按照我的教授的要求分成了两个类。我在下面包含了这两个类。我会非常感谢您能提供的所有帮助。谢谢!
GuessingGame.java:主类
public class GuessingGame {
public static void main(String[] args) {
new Guess().doGuess();
}
}
Guess.java:
class Guess {
private int answer = 0;
int tries = 0;
Scanner input = new Scanner(System.in);
int guess, i;
boolean win = false;
int amount = 10;
public Guess() {
answer = generateRandomNumber();
}
// 生成一个1到一千之间的私有数字
private int generateRandomNumber() {
Random rand = new Random();
return rand.nextInt(1000) + 1;
}
public void doGuess() {
while(!win) {
System.out.println("你只有十次尝试机会。" +
"猜一个1到1000之间的数字:");
guess = input.nextInt();
if (guess > 1000) {
System.out.println("你猜的数字超出范围!");
} else if (guess < 1) {
System.out.println("你猜的数字超出范围!");
} else if (guess == answer) {
win = true;
tries++;
} else if (guess < answer && i != amount - 1) {
System.out.println("你猜的数字太低!");
tries++;
} else if (guess > answer && i != amount - 1) {
System.out.println("你猜的数字太高!");
tries++;
}
}
System.out.println("恭喜!你猜对了数字!" +
"数字是:" + answer);
System.out.println("你用了 " + tries + " 次尝试");
}
}
英文:
I cannot figure out how to add a limit to the number of guesses in my number guessing game. I have tried adding a for statement after the while statement but that made the code just stop at the tenth guess and there was no winner ever. I deleted the while statement and just did the for statement which ensured that the user got the correct answer every ninth guess. My code is divided into two classes as requested by my professor. I am including both below. I would appreciate all the help I can get. Thank you!
GuessingGame.java: Main Class
public class GuessingGame {
public static void main(String[] args) {
new Guess().doGuess();
}
}
Guess.java
class Guess {
private int answer = 0;
int tries = 0;
Scanner input = new Scanner(System.in);
int guess, i;
boolean win = false;
int amount = 10;
public Guess() {
answer = generateRandomNumber();
}
//Generate a private number between 1 and a thousand
private int generateRandomNumber() {
Random rand = new Random();
return rand.nextInt(1000) + 1;
}
public void doGuess() {
while(!win) {
System.out.println("You are limited to ten attempts."
+ "Guess a number between 1 and 1000: ");
guess = input.nextInt();
if (guess > 1000 ) {
System.out.println("Your guess is out of the range!");
} else if (guess < 1) {
System.out.println("Your guess is out of the range!");
} else if (guess == answer) {
win = true;
tries++;
} else if (guess < answer && i != amount -1) {
System.out.println("Your guess is too low!");
tries++;
} else if (guess > answer && i != amount -1) {
System.out.println("Your guess is too high!");
tries++;
}
}
System.out.println("Congragulations! You guessed the number!"
+ "The number was: " +answer);
System.out.println("It took you " + tries + " tries");
}
}
答案1
得分: 0
你可以在while循环内部添加一个if语句。
public void doGuess() {
while(!win) {
System.out.println("您最多有十次尝试。"
+ "猜一个介于1和1000之间的数字:");
guess = input.nextInt();
if(tries > 9) {
...无论当用户达到10次猜测时您想要发生什么...
}
英文:
You can add an if-statement inside the while loop.
public void doGuess() {
while(!win) {
System.out.println("You are limited to ten attempts."
+ "Guess a number between 1 and 1000: ");
guess = input.nextInt();
if(tries > 9) {
...whatever you want to happen when user has reached 10 guesses...
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论