简体   繁体   English

最小值和最大值

[英]Minimum and maximum value

I ran this code, everything finds.我运行了这段代码,一切都找到了。 maximumValue shows the correct output, however, minValue always shows 0 as the output. maximumValue显示正确的输出,但是, minValue始终显示 0 作为输出。

Scanner sc = new Scanner(System.in);

int x= sc.nextInt();

int[] day =  new int[x];

int minValue = day[0]; 
int maxValue = day[0];

for (int i = 0; i < x; i++) {
    day[i] = sc.nextInt();

    if (day[i] < minValue) minValue = day[i];
    if (day[i] > maxValue) maxValue = day[i];
}

System.out.println(minValue);
System.out.println(maxValue);

Why it does not work为什么它不起作用

You read day[0] before you assign it any value.在为它分配任何值之前,您先阅读day[0]

Your code你的代码

int[] day =  new int[x];                                 
int minValue = day[0]; 
int maxValue = day[0];

is equivalent to相当于

int[] day =  new int[x];                                 
int minValue = 0; 
int maxValue = 0;

See https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.5 :请参阅https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.5

Each ... array component is initialized with a default value when it is created.每个 ... 数组组件在创建时都使用默认值进行初始化。

For type int, the default value is zero, that is, 0.对于 int 类型,默认值为 0,即 0。

You code works for max only by accident :) Try entering only negative values to the input and your max will also give only 0 .您的代码仅在偶然情况下适用于max :) 尝试在输入中输入负值,您的max也只会给出0

How to fix it如何修复

It is highly recommendable to split the computational part from the input/output.强烈建议将计算部分与输入/输出分开。 Java cannot easily return two values from a method. Java 不能轻易地从一个方法返回两个值。 You may implement two separate methods, for max and min separately, or you may return the two values in an ad-hoc created Tuple object.您可以分别为maxmin实现两个单独的方法,或者您可以在临时创建的 Tuple 对象中返回这两个值。

The input/output part:输入/输出部分:

final Scanner sc = new Scanner(System.in);

final int numberOfValues = sc.nextInt();
final int[] values =  new int[numberOfValues];
for (int i = 0; i < numberOfValues; i++) {
    values[i] = sc.nextInt();
}

System.out.println(minValue(values));
System.out.println(maxValue(values));

And the computing part (look at the for-each loop https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html ):和计算部分(查看 for-each 循环https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html ):

public int minValue(int[] array) {
    int min = Integer.MAX_VALUE;
    for (int value : array) {
        if (value < min) {
            min = value;
        }
    }
}

Analogically for maxValue() , you will now easily do it on your own.maxValue() ,您现在可以轻松地自己完成。

Your program still needs some error handling, such as when the user provides inconsistent input.您的程序仍然需要一些错误处理,例如当用户提供不一致的输入时。

You should get the habit of splitting the computational and presentational part of your program from the very beginning of your programming carrier :) You will produce better maintainable programs and you will also see your errors much faster.您应该养成从编程载体一开始就将程序的计算部分和表示部分分开的习惯:) 您将生成更好的可维护程序,并且您还将更快地看到错误。

You can do it like this.你可以这样做。

Scanner sc = new Scanner(System.in);

int x= sc.nextInt();
int[] day =  new int[x];

for (int i = 0; i < x; i++) {
    day[i] = sc.nextInt();
}
int minValue = day[0]; 
int maxValue = day[0];

for (int i = 0; i < x; i++) {
    if (day[i] < minValue) minValue = day[i];
    if (day[i] > maxValue) maxValue = day[i];
}

System.out.println(minValue);
System.out.println(maxValue);
    

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

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