如何在用户输入的数组中找到最小的元素?为什么显示为零的是最小元素?

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

How to find the smallest element in an user inputted array? Why the smallest element is displayed zero?

问题

以下是翻译好的代码部分:

import java.util.Scanner;

public class Average {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("请输入元素的数量:");
        int length = input.nextInt();

        int[] num = new int[length];
        System.out.println("请输入 " + length + " 个数组元素:");
        int sum = 0;
        int large, small;
        large = small = num[0];

        for (int i = 0; i < length; i++) {
            num[i] = input.nextInt();
            sum = sum + num[i];
        }

        for (int i = 0; i < length; ++i) {
            if (num[i] < small) {
                small = num[i];
            }

            if (num[i] > large) {
                large = num[i];
            }
        }

        double avg = sum / length;
        System.out.println("总和为:" + sum);
        System.out.println("平均值为:" + avg);
        System.out.println("最小元素为:" + small);
        System.out.println("最大元素为:" + large);
    }
}
英文:

The code displays the sum, average, and largest element. It doesn't display the smallest element as the output is always zero. How do I display the smallest element in the array?

import java.util.Scanner;
public class Average {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(&quot;Enter the number of elements:&quot;);
int length = input.nextInt();
int[] num = new int[length];
System.out.println(&quot;Enter the &quot;+ length +  &quot; array elements:&quot;);
int sum = 0;
int large,small;
large =small = num[0]; 
for (int i=0; i&lt;length;i++) {
num[i] = input.nextInt();
sum = sum+ num[i];
}
for (int i=0; i&lt;length; ++i) {
if (num[i]&lt;small) {
small = num[i];
}
if(num[i]&gt; large) {
large = num[i];
}
}
double avg = sum/length;
System.out.println(&quot;The sum is &quot;+ sum);
System.out.println(&quot;The average is &quot;+ avg);
System.out.println(&quot;The smallest element is &quot;+ small);
System.out.println(&quot;The largest element is &quot;+ large);
}
}

答案1

得分: 0

是的,这是因为默认情况下,您的字段 small 等于 0。

现在让我们逐步看一下您的 if 语句

        if (num[i] < small) {
             small = num[i];
        }

以数字 22、11、6 为例:

所以第一步是 num[0] < 0,为什么是 0,因为之前提到 small = 0 默认值
第二步是 num[1] < 0,small 仍然保持为 0
第三步是 num[2] < 0,small 仍然保持为 0

您忽略了什么,您需要在 for 循环的第一次迭代中为 small 赋值,例如:

       for (int i=0; i<length; ++i) {
            if(i == 0){
                small = num[i];
            }
            if (num[i] < small) {
                small = num[i];
            }

            if(num[i] > large) {
                large = num[i];
            }
        }

现在您的程序应该可以正常工作 如何在用户输入的数组中找到最小的元素?为什么显示为零的是最小元素?

英文:

Yea, it's happening because by default your field small is equal to 0.

And now let's look step by step your if statement

        if (num[i]&lt;small) {
small = num[i];
}

example for numbers: 22,11,6,

So first step is num[0] &lt; 0, why 0 as mention before small = 0 by default
Step two num[1] &lt; 0, small stills stay 0
Step Three num[2] &lt; 0, small stills stay 0.

What are you missing, you need to assign value to small at the first iteration of your for loop, for example:

       for (int i=0; i&lt;length; ++i) {
if(i == 0){
small = num[i];
}
if (num[i]&lt;small) {
small = num[i];
}
if(num[i]&gt; large) {
large = num[i];
}
}

Now your program should work 如何在用户输入的数组中找到最小的元素?为什么显示为零的是最小元素?

答案2

得分: 0

import java.util.Scanner;

class Average {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("输入元素个数:");
        int length = input.nextInt();

        int[] num = new int[length];
        System.out.println("输入 " + length + " 个数组元素:");
        int sum = 0;
        int large, small;

        for (int i = 0; i < length; i++) {
            num[i] = input.nextInt();
            sum = sum + num[i];
        }

        large = small = num[0]; // 小的应该在输入 num 后赋值

        for (int i = 0; i < length; ++i) {
            if (num[i] < small) {
                small = num[i];
            }

            if (num[i] > large) {
                large = num[i];
            }
        }

        double avg = sum / length;
        System.out.println("总和是 " + sum);
        System.out.println("平均值是 " + avg);
        System.out.println("最小的元素是 " + small);
        System.out.println("最大的元素是 " + large);

    }
}
英文:
import java.util.Scanner;

class Average {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println(&quot;Enter the number of elements:&quot;);
        int length = input.nextInt();

        int[] num = new int[length];
        System.out.println(&quot;Enter the &quot; + length + &quot; array elements:&quot;);
        int sum = 0;
        int large, small;

        for (int i = 0; i &lt; length; i++) {
            num[i] = input.nextInt();
            sum = sum + num[i];
        }

        large = small = num[0]; // small should be assigned after num is input
        
        for (int i = 0; i &lt; length; ++i) {
            if (num[i] &lt; small) {
                small = num[i];
            }

            if (num[i] &gt; large) {
                large = num[i];
            }
        }

        double avg = sum / length;
        System.out.println(&quot;The sum is &quot; + sum);
        System.out.println(&quot;The average is &quot; + avg);
        System.out.println(&quot;The smallest element is &quot; + small);
        System.out.println(&quot;The largest element is &quot; + large);

    }
}

答案3

得分: 0

如果您使用Java 8或更高版本您可以使用Arrays的流方法来简化数组的操作[更多信息][1]

首先导入Arrays:`import java.util.Arrays;`

System.out.println("数组元素的和为:" + Arrays.stream(num).sum());
System.out.println("数组元素的平均值为:" + Arrays.stream(num).average());
System.out.println("数组中最小的元素为:" + Arrays.stream(num).min());
System.out.println("数组中最大的元素为:" + Arrays.stream(num).max());

[1]: https://docs.oracle.com/javase/8/docs/api/java/util/stream/IntStream.html
英文:

if you use java 8 or above, you can use the stream method of Arrays. it simplifies work with arrays. for more info

firstly import the Arrays as import java.util.Arrays;

System.out.println(&quot;The sum is &quot; + Arrays.stream(num).sum());
System.out.println(&quot;The average is &quot; + Arrays.stream(num).average());
System.out.println(&quot;The smallest element is &quot; + Arrays.stream(num).min());
System.out.println(&quot;The largest element is &quot; + Arrays.stream(num).max());

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

发表评论

匿名网友

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

确定