简体   繁体   中英

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.

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

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

example for numbers: 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 (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. it simplifies work with arrays. for more info

firstly import the Arrays as 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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