简体   繁体   English

用户输入从不进入 if 语句

[英]User input never enters if statement

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.该程序的要点是让用户无休止地输入整数数量(直到他们输入的不是整数),并且对于用户输入的每个 integer,它应该检查 integer 是否大于或小于输入的值.

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.此时,用户输入他们的 integer,但从未在以下 if 语句中输入。 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("Enter number: ");
            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("Your minimum number: " + number);
                System.out.println("Your maximum number: " + maxNumber);
                break;
            }
        } while (true);

        scanner.close();
    }
}

Your minNumber and maxNumber declarations should be out side of the loop.您的minNumbermaxNumber声明应该在循环之外。 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 !在 print 语句而不是minNumber中,您正在打印number

It's not reaching the if statements, because if it did, the user input would update to the value entered I would think.它没有到达 if 语句,因为如果它到达了,用户输入将更新为我认为输入的值。 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.您没有得到正确的 output 并且您假设原因是代码未输入if语句。 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.如果您将打印输出放在if语句中,您会看到它们确实在运行。 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("Enter number: ");
    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 < number) {
            maxNumber = number;
        }
        if (minNumber > number) {
            minNumber = number;
        }
    } else {
        System.out.println("Your minimum number: " + number);
        System.out.println("Your maximum number: " + maxNumber);
        break;
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM