gpt4 book ai didi

java - 将范围保持在一组特定的数字内

转载 作者:行者123 更新时间:2023-12-01 17:16:11 25 4
gpt4 key购买 nike

对于我的 Java 类,我正在编写一个小程序,它首先选择 1 到 100 之间的 int 数字。然后提示用户开始猜测正确的 int >。如果用户猜测的 int 值过高或过低,程序会打印出一个新的范围供他们猜测。如果用户输入一个String或一个double,程序只是重新要求用户输入一个int,但不改变范围反正。

示例输出(当 secret 数字为 20 时)如下所示:

c:\csc116> java GuessingGame Guess the secret number!Enter a number between 1 and 100 (inclusive): 45Enter a number between 1 and 44 (inclusive): jlkjEnter a number between 1 and 44 (inclusive): 31.0  //doubleEnter a number between 1 and 44 (inclusive): 1000 //outside the range of 1-100Enter a number between 1 and 44 (inclusive): 34Enter a number between 1 and 33 (inclusive): 15Enter a number between 16 and 33 (inclusive): 20    You win!

The program appears to be almost there, but with one exception. One of the requirements is that when a user types an int that is outside our given range of 1 and 100, the print out message does not change (as seen in example above). This is where I am getting stumped quite a bit, and am looking to see if anyone can help guide me to the correct answer.

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!");
}
}

最佳答案

你有:

            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) {
// ...
}

关于java - 将范围保持在一组特定的数字内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21999221/

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