简体   繁体   English

为什么我的“为什么”循环中的“如果”语句不能更改我的一个对象?

[英]Why does my 'if' statement in my 'why' loop not change one of my objects?

I'm brand new to Java and creating a simple program that takes information output from another program and uses it via command line for a loop. 我是Java的新手,并创建了一个简单的程序,该程序将从另一个程序获取信息并通过命令行将其用于循环。 No matter how many times I look at it, I can't figure out why the 'if (minValue > value' doesn't change minValue. 不管我看多少次,我都无法弄清楚为什么'if(minValue> value'不会改变minValue。

Output given some information that should probably produce a minValue: 给定某些可能会产生minValue的信息的输出:

Count:3
Minimum: 0 @
Maximum: 75 @ DummyDate3 DummyTime3
Average: 45.00

Is this a result of the while loop? 这是while循环的结果吗?

int minValue = 0;
    int maxValue = 0;
    String minValueTime = "";
    String minValueDate = "";
    String maxValueDate = "";
    String maxValueTime = "";
    int count = 0;
    double average = 0;

    /*
     * For as long as input is going through A date, a time, and a value
     * will come through as a loop If the minimum value is less than the
     * value coming through The minimum value will become the value. If the
     * maximum value is less than the value, the maximum value will become
     * the value.
     */
    while (input.hasNext() == true) {
        String date = (input.next());
        String time = (input.next());
        int value = (input.nextInt());
        if (minValue > value) {
            minValue = value;
            minValueDate = date;
            minValueTime = time;
        }
        if (maxValue < value) {
            maxValue = value;
            maxValueDate = date;
            maxValueTime = time;
        }
        count++;
        average = average + value;
    }

    input.close();

    System.out
            .printf("Count:%d%nMinimum: %d @ %s %s%nMaximum: %d @ %s %s%nAverage: %.2f%n",
                    count, minValue, minValueDate, minValueTime, maxValue,
                    maxValueDate, maxValueTime, average / count);
}

EDIT: I have tried Integer.MAX_VALUE and MIN_VALUE, but both of them result in the values of MAX_VALUE and MIN_VALUE, respectively. 编辑:我已经尝试了Integer.MAX_VALUE和MIN_VALUE,但是它们两个分别导致MAX_VALUE和MIN_VALUE的值。

The first value assigned to min is 0. If every integer read is bigger than 0, it will never change. 分配给min的第一个值是0。如果每个读取的整数都大于0,则它将永远不变。 What you actually need to do is assing max and min to the minValue and maxValue : 您实际需要做的是将max和min赋给minValue和maxValue:

int minValue = Integer.MAX_VALUE;
int maxValue = Integer.MIN_VALUE;

This way, the first value read will most likely be lower than the minimum at the moment and the first value will also be higher than the current max value. 这样,读取的第一个值很可能会低于当前的最小值,并且第一个值也将高于当前的最大值。

The min value can't go higher than this with those values, the max can't go lower than this too. 最小值不能超过这些值,最大值也不能低于此值。

The above answer using Integer.MAX_VALUE and Integer.MIN_VALUE works fine, but another technique used frequently if you know the array or input you are looping through has at least one entry is to initialize minValue and maxValue to the first entry. 上面使用Integer.MAX_VALUEInteger.MIN_VALUE答案很好用,但是如果您知道要循环的数组或输入具有至少一个条目,则经常使用的另一种技术是将minValuemaxValue初始化为第一个条目。

eg 例如

int firstEntry = input.nextInt();
int minValue = firstEntry;
int maxValue = firstEntry;

while (input.hasNext()) {
    int currentVal = input.nextInt();
    minValue = Math.min(minValue, currentVal);
    maxValue = Math.max(maxValue, currentVal);
}

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

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