对于我的Java类,我正在编写一个小程序,首先选择1到100之间的int
数字。然后它会提示用户开始猜测正确的int
。如果用户猜测int
太高或太低,程序会打印出一个新的范围供他们猜测。如果用户输入String
或double
,程序只会重新要求用户输入int
,但不会以任何方式更改范围。
示例输出(当密码是20时)看起来像:
c:\csc116> java GuessingGame Guess the secret number! Enter a number between 1 and 100 (inclusive): 45 Enter a number between 1 and 44 (inclusive): jlkj Enter a number between 1 and 44 (inclusive): 31.0 //double Enter a number between 1 and 44 (inclusive): 1000 //outside the range of 1-100 Enter a number between 1 and 44 (inclusive): 34 Enter a number between 1 and 33 (inclusive): 15 Enter a number between 16 and 33 (inclusive): 20 You win!
该程序似乎几乎存在,但有一个例外。其中一个要求是,当用户键入超出我们给定范围1和100的int
时,打印输出消息不会更改(如上例所示)。这是我很难得到的地方,我正在寻找是否有人可以帮助指导我找到正确的答案。
import java.util.*;
public class GuessingGame {
public static void main(String[] args) {
introduction();
Scanner console = new Scanner(System.in);
Random rand = new Random();
int guess = 0;
int minimum = 1;
int maximum = 100;
int secretNumber = rand.nextInt(100) + 1;
System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): ");
while (guess != secretNumber) {
if (console.hasNextInt()) {
guess = console.nextInt();
if (guess > secretNumber) {
maximum = guess - 1;
System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): ");
}
if (guess < secretNumber) {
minimum =guess + 1;
System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): ");
}
if (guess == secretNumber) {
System.out.println("You win!");
}
} else {
console.next();
System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): ");
}
}
}
public static void introduction() {
System.out.println("Guess the secret number!");
}
}
答案 0 :(得分:2)
你有:
guess = console.nextInt();
if (guess > secretNumber) {
maximum = guess - 1;
System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): ");
}
if (guess < secretNumber) {
minimum =guess + 1;
System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): ");
}
if (guess == secretNumber) {
System.out.println("You win!");
}
现在你根本没有检查最小/最大范围。您将不得不为此添加一个明确的检查,但警告(在此处的其他答案中遗漏)是您必须确保不处理输入作为猜测它是否已经出来范围。您目前使用if
而没有else
的样式意味着在实施时必须小心。你有几个选择,例如:
guess = console.nextInt();
if (guess < minimumAllowed || guess > maximumAllowed) {
// handle error
} else {
// handle valid input
if (guess > secretNumber) {
// ...
}
if (guess < secretNumber) {
// ...
}
if (guess == secretNumber) {
// ...
}
}
或者:
guess = console.nextInt();
if (guess < minimumAllowed || guess > maximumAllowed) {
// handle error
} else if (guess > secretNumber) {
// ...
} else if (guess < secretNumber) {
// ...
} else if (guess == secretNumber) {
// ...
}
或者,坚持你当前的风格,只要你不必在循环中做任何更多不相关的逻辑(在程序中似乎就是这种情况):
guess = console.nextInt();
if (guess < minimumAllowed || guess > maximumAllowed) {
// handle error
continue;
}
// handle valid input
if (guess > secretNumber) {
// ...
}
if (guess < secretNumber) {
// ...
}
if (guess == secretNumber) {
// ...
}
答案 1 :(得分:1)
如果您还希望将用户提示为原始最小值和最大值,则应分别保留这两个值并插入另一个if
- 在循环开始时检查
guess = console.nextInt();
if (guess > originalMaximum) {
System.out.print("Enter a number less then " + originalMaximum);
}
答案 2 :(得分:1)
您只是检查相对于密码的猜测号码。您缺少的是检查相对于最大值和最小值的猜测数字。例如:
if (guess > maximum) {
System.out.print("Too high!");
} else if (guess < minimum) {
System.out.print("Too low!");
}
答案 3 :(得分:1)
在您检查猜到的号码是否少于或多于秘密号码之前,请在此之前进行此检查:
if (guess < minimum || guess > maximum)
{
System.out.print("Please enter a number between " + minimum + " and " + maximum);
continue;
}
if (guess > secretNumber)
{
maximum = guess - 1;
System.out.print("Enter a number between " + minimum + " and " + maximum + " (inclusive): ");
}