这是我迄今为止制定的准则。
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
请让我了解任何建议。