简体   繁体   English

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

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

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("Enter the number of elements:");
        int length = input.nextInt();
        
        int[] num = new int[length];
        System.out.println("Enter the "+ length +  " array elements:");
        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("The sum is "+ sum);
        System.out.println("The average is "+ avg);
        System.out.println("The smallest element is "+ small);
        System.out.println("The largest element is "+ large);
        }
}

Yea, it's happening because by default your field small is equal to 0.是的,这是因为默认情况下您的字段 small 等于 0。

And now let's look step by step your if statement现在让我们一步一步地看看你的 if 语句

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

example for numbers: 22,11,6,数字示例:22,11,6,

So first step is num[0] < 0, why 0 as mention before small = 0 by default
Step two num[1] < 0, small stills stay 0
Step Three num[2] < 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 循环的第一次迭代中将值分配给 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];
            }
        }

Now your program should work :)现在你的程序应该可以工作了:)

import java.util.Scanner; class Average { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter the number of elements:"); int length = input.nextInt(); int[] num = new int[length]; System.out.println("Enter the " + length + " array elements:"); 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]; // small should be assigned after num is input 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("The sum is " + sum); System.out.println("The average is " + avg); System.out.println("The smallest element is " + small); System.out.println("The largest element is " + large); } }

if you use java 8 or above, you can use the stream method of Arrays.如果你使用java 8或更高版本,你可以使用Arrays的stream方法。 it simplifies work with arrays.它简化了数组的工作。 for more info 欲了解更多信息

firstly import the Arrays as import java.util.Arrays;首先将数组import java.util.Arrays;import java.util.Arrays;

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

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

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