用户输入永远不会进入if语句。

huangapple go评论74阅读模式
英文:

User input never enters if statement

问题

程序的目的是让用户无限次输入整数(直到输入非整数为止),并针对用户输入的每个整数检查它是否大于或小于输入的值。

问题:当程序运行时,一切都正常,直到达到以下部分:

number = scanner.nextInt();

在这一点上,用户输入他们的整数,但从未进入以下的if语句。我希望得到一个提示而不是答案,但我会接受任何帮助。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        do {
            System.out.println("输入数字:");
            int number = 0;
            int minNumber = 0;
            int maxNumber = 0;
            boolean hasInt = scanner.hasNextInt();
            if (hasInt) {
                number = scanner.nextInt();
                if (maxNumber < number) {
                    maxNumber = number;
                }
                if (minNumber > number) {
                    minNumber = number;
                }
            } else {
                System.out.println("您的最小数字:" + number);
                System.out.println("您的最大数字:" + maxNumber);
                break;
            }
        } while (true);

        scanner.close();
    }
}
英文:

The point of the program is to have a user input an amount of integers endlessly (until they enter something other than an integer), and for each integer the user inputs, it should check if the integer is greater than or less than the value entered.

The problem: When the program runs, everything is fine until reaching

number = scanner.nextInt();

At this point, the user inputs their integer, but never makes it inside the following if statements. I would love a hint instead of an answer, but I'll take what I can get.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        do {
            System.out.println(&quot;Enter number: &quot;);
            int number = 0;
            int minNumber = 0;
            int maxNumber = 0;
            boolean hasInt = scanner.hasNextInt();
            if (hasInt) {
                number = scanner.nextInt();
                if (maxNumber &lt; number) {
                    maxNumber = number;
                }
                if (minNumber &gt; number) {
                    minNumber = number;
                }
            } else {
                System.out.println(&quot;Your minimum number: &quot; + number);
                System.out.println(&quot;Your maximum number: &quot; + maxNumber);
                break;
            }
        } while (true);

        scanner.close();
    }
}

答案1

得分: 4

Your minNumber and maxNumber declarations should be outside of the loop. Also, you need to initialize the values as below to get correct min and max comparison with the entered values only:

int minNumber = Integer.MAX_VALUE;
int maxNumber = Integer.MIN_VALUE;

In the print statement, instead of minNumber, you are printing number!

英文:

Your minNumber and maxNumber declarations should be out side of the loop. Also, you need to initialize the values as below to get correct min and max comparison with the entered values only:

int minNumber = Integer.MAX_VALUE;
int maxNumber = Integer.MIN_VALUE;

In print statement instead of minNumber you are printing number!

答案2

得分: 1

以下是翻译好的部分:

> It's not reaching the if statements, because if it did, the user input would update to the value entered I would think. It doesn't. It outputs the values initially declared.
“它没有达到if语句,因为如果它这样做了,用户输入将会更新为输入的值,我认为是这样的。但它没有。它输出最初声明的值。”

You're not getting the right output and you have a hypothesis that the cause is the code not entering the if statements. Following the scientific method, the next step is to test your hypothesis.
你没有得到正确的输出,并且你有一个假设,即原因是代码没有进入if语句。按照科学方法,下一步是测试你的假设。

If you put printouts inside the if statements you'll see that they are indeed running. That's not it. The mistake must be elsewhere. You should collect more evidence and develop a new hypothesis.
如果你在if语句中放置打印输出,你会看到它们确实在运行。但问题不在这里。错误必须在其他地方。你应该收集更多的证据并提出一个新的假设。

Hint: Try printing out the values of your variables at the beginning and end of each iteration. I've marked the places below. Are they what you expect them to be? You're going to see an anomaly that should point you in the right direction.
**提示:**尝试在每次迭代的开头和结尾打印出变量的值。我在下面标出了位置。它们是否符合你的预期?你将会看到一个异常情况,这应该会指引你走向正确的方向。

do {
    System.out.println("Enter number: ");
    int number = 0;
    int minNumber = 0;
    int maxNumber = 0;

    // 打印 number、minNumber 和 maxNumber 的值。

    boolean hasInt = scanner.hasNextInt();
    if (hasInt) {
        number = scanner.nextInt();
        if (maxNumber < number) {
            maxNumber = number;
        }
        if (minNumber > number) {
            minNumber = number;
        }
    } else {
        System.out.println("Your minimum number: " + number);
        System.out.println("Your maximum number: " + maxNumber);
        break;
    }

    // 打印 number、minNumber 和 maxNumber 的值。
} while (true);
英文:

> It's not reaching the if statements, because if it did, the user input would update to the value entered I would think. It doesn't. It outputs the values initially declared.

You're not getting the right output and you have a hypothesis that the cause is the code not entering the if statements. Following the scientific method, the next step is to test your hypothesis.

If you put printouts inside the if statements you'll see that they are indeed running. That's not it. The mistake must be elsewhere. You should collect more evidence and develop a new hypothesis.

Hint: Try printing out the values of your variables at the beginning and end of each iteration. I've marked the places below. Are they what you expect them to be? You're going to see an anomaly that should point you in the right direction.

do {
    System.out.println(&quot;Enter number: &quot;);
    int number = 0;
    int minNumber = 0;
    int maxNumber = 0;

    // Print number, minNumber, and maxNumber.

    boolean hasInt = scanner.hasNextInt();
    if (hasInt) {
        number = scanner.nextInt();
        if (maxNumber &lt; number) {
            maxNumber = number;
        }
        if (minNumber &gt; number) {
            minNumber = number;
        }
    } else {
        System.out.println(&quot;Your minimum number: &quot; + number);
        System.out.println(&quot;Your maximum number: &quot; + maxNumber);
        break;
    }

    // Print number, minNumber, and maxNumber.
} while (true);

huangapple
  • 本文由 发表于 2020年8月9日 00:35:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63317795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定