这是我目前拥有的代码。
import java.util.Scanner;
import java.util.Random;
public class GuessANumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a random seed: ");
int seed = scanner.nextInt();
Random rnd = new Random (seed);
int rightAnswer = rnd.nextInt(200)+1;
int numAttempts = 0;
System.out.print("Enter a guess between 1 and 200: ");
int guess = scanner.nextInt();
numAttempts ++;
while (guess != rightAnswer) {
if (guess < 1 || guess > 200) {
System.out.println("Your guess is out of range. Pick a number between 1 and 200.");
}
if (guess > rightAnswer) {
System.out.println("Your guess was too high - try again.");
}
else if (guess < rightAnswer) {
System.out.println("Your guess was too low - try again.");
}
System.out.println();
System.out.print("Enter a guess between 1 and 200: ");
guess = scanner.nextInt();
numAttempts ++;
}
if (guess == rightAnswer) {
System.out.println("Congratulations! Your guess was correct!");
System.out.println();
System.out.println("I had chosen " + rightAnswer + " as the target number.");
System.out.println("You guessed it in " + numAttempts + " tries.");
}
if (numAttempts == 1) {
System.out.println("That was impossible!");
}
else if (numAttempts == 2 || numAttempts == 3) {
System.out.println("You're pretty lucky!");
}
else if (numAttempts >= 4 && numAttempts <= 7) {
System.out.println("Not bad, not bad...");
}
else if (numAttempts == 8) {
System.out.println("That was not very impressive.");
}
else if (numAttempts == 9 || numAttempts == 10) {
System.out.println("Are you having any fun at all?");
}
else if (numAttempts >= 11) {
System.out.println("Maybe you should play something else.");
}
scanner.close();
}
}
这就是输出应该是的:
Enter a random seed: Enter a guess between 1 and 200: Your guess is out of range. Pick a number between 1 and 200.
Your guess was too low - try again.
Enter a guess between 1 and 200: Your guess was too low - try again.
Enter a guess between 1 and 200: Your guess is out of range. Pick a number between 1 and 200.
Your guess was too high - try again.
但是,我的代码在此测试中提供的输出有一行不应该存在的额外空格,如下所示:
Enter a random seed: Enter a guess between 1 and 200: Your guess is out of range. Pick a number between 1 and 200.
Your guess was too low - try again.
Enter a guess between 1 and 200: Your guess was too low - try again.
Enter a guess between 1 and 200: Your guess is out of range. Pick a number between 1 and 200.
Your guess was too high - try again.
E
如有任何建议请告诉我!
if (guess == rightAnswer) {
应该位于 in 你的 while
循环体中。此外,它还需要一个break
。我还建议使用 do-while
循环,因为您可以将提示放入循环中。我也不会将无效的猜测算作尝试。把所有这些放在一起,它可能看起来像
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a random seed: ");
int seed = scanner.nextInt();
Random rnd = new Random(seed);
int rightAnswer = rnd.nextInt(200) + 1;
int numAttempts = 0;
do {
System.out.println("Enter a guess between 1 and 200: ");
int guess = scanner.nextInt();
numAttempts++;
if (guess < 1 || guess > 200) {
System.out.println("Your guess is out of range. Pick a number between 1 and 200.");
numAttempts--;
} else if (guess > rightAnswer) {
System.out.println("Your guess was too high - try again.");
} else if (guess < rightAnswer) {
System.out.println("Your guess was too low - try again.");
} else if (guess == rightAnswer) {
System.out.println("Congratulations! Your guess was correct!");
System.out.println();
System.out.println("I had chosen " + rightAnswer + " as the target number.");
System.out.println("You guessed it in " + numAttempts + " tries.");
break;
}
} while (true);
if (numAttempts == 1) {
System.out.println("That was impossible!");
} else if (numAttempts <= 3) {
System.out.println("You're pretty lucky!");
} else if (numAttempts <= 7) {
System.out.println("Not bad, not bad...");
} else if (numAttempts == 8) {
System.out.println("That was not very impressive.");
} else if (numAttempts <= 10) {
System.out.println("Are you having any fun at all?");
} else {
System.out.println("Maybe you should play something else.");
}