在java中从数组中查找最小值

我正在尝试从数组中的用户输入中获取总和、平均值、最大值和最小值。sum、average 和 max 给出了正确的输出。但是最小值不起作用。我在哪里做错了有人会帮助我找出答案吗?


import java.util.Scanner;


public class minMaxSumAverage {

    public static void main(String args[]) {

        int n, sum = 0, max, min;

        double average = 0;


        Scanner s = new Scanner(System.in);

        System.out.println("Enter elements you want to input in array: ");

        n = s.nextInt();

        int a[] = new int[n];

        max = a[0];

        min = a[0];

        System.out.println("Enter all the elements:");

        for (int i = 0; i < n; i++) {

            a[i] = s.nextInt();

            sum += a[i];

            average = (double) sum/a.length;

            if (a[i] > max) {

                max = a[i];

            }

            if (a[i] < min) {

                min = a[i];

            }

        }

        System.out.println("Sum is: " + sum);

        System.out.println("Average is: " + average);

        System.out.println("Max is: " + max);

        System.out.println("Min is: " + min);

    }

}

输出:


Enter elements you want to input in array: 

5

Enter all the elements:

25

5

10

6

4

Sum is: 50

Average is: 10.0

Max is: 25

Min is: 0

最小值应为 4。


一只甜甜圈
浏览 215回答 6
6回答

小唯快跑啊

我已经更新了你的代码。请检查以下代码以从所有元素列表中获取最小值。输入 :在数组中输入要输入的元素:5输入所有元素:2551064输出 :总数是:50平均值为:10.0最大值为:25最小值为:4&nbsp; &nbsp; &nbsp; &nbsp; Scanner scan = null;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int n, sum = 0, max, min;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double average = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scan = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter elements you want to input in array: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n = scan.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int a[] = new int[n];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = a[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter all the elements:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[i] = scan.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += a[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; average = (double) sum/a.length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (a[i] > max) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = a[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // from here remove logic for get min value.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (a[i] < min) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;min = a[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; **/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min = a[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0;i<a.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(a[i] < min){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min = a[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Sum is: " + sum);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Average is: " + average);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Max is: " + max);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Min is: " + min);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }finally{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scan.close();&nbsp; &nbsp; &nbsp; &nbsp; }

Qyouu

从Java-8开始,您可以使用流在一行中完成此操作:int[]&nbsp;a&nbsp;=&nbsp;new&nbsp;int[]&nbsp;{&nbsp;20,11,2,3,4,7,8,90&nbsp;}; int&nbsp;min&nbsp;=&nbsp;Arrays.stream(a).min().getAsInt();要获取最大元素,只需将其替换.min()为.max()获得总和:Arrays.stream(a).mapToInt(Integer::intValue).sum();

湖上湖

作为替代方案,您可能需要考虑 Stream...public static void main(String args[]) {&nbsp; &nbsp; Scanner s = new Scanner(System.in);&nbsp; &nbsp; System.out.println("Enter count elements you want to input in array: ");&nbsp; &nbsp; int n = s.nextInt();&nbsp; &nbsp; int a[] = new int[n];&nbsp; &nbsp; System.out.println("Enter all the elements:");&nbsp; &nbsp; for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; a[i] = s.nextInt();&nbsp; &nbsp; }&nbsp; &nbsp; s.close();&nbsp; &nbsp; IntSummaryStatistics iss = Arrays.stream(a).summaryStatistics();&nbsp; &nbsp; System.out.println("Sum is: " + iss.getSum());&nbsp; &nbsp; System.out.println("Average is: " + iss.getSum()/iss.getCount());&nbsp; &nbsp; System.out.println("Max is: " + iss.getMax());&nbsp; &nbsp; System.out.println("Min is: " + iss.getMin());}

料青山看我应如是

因为 min 初始化为 0。在你的循环中,你只需要询问你当前的数字是否小于 0。再次尝试将数组的第一个值指定为最小值。然后它应该工作。

子衿沉夜

&nbsp;int a[] = new int[n];&nbsp;max = a[0];&nbsp;min = a[0];您已经创建了一个空数组a。max 和 min 都初始化为 0。这对 max 很好用,但给你的结果是你看到的 min 的结果,因为在你创建的数组中没有比零更小的东西。要解决此问题,请 min = a[0];在用值填充数组后将行添加到 for 循环中,例如for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[i] = s.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += a[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; average = (double) sum/a.length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; min = a[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if-statements here然后,min = a[0]将不再为零,而是填充数组的第一个值。

至尊宝的传说

请参考下面的代码,了解最小、最大、总和、平均功能:List<Integer> intList= Arrays.asList(70, 80, 10, 20, 40, 50);&nbsp; &nbsp; Integer ans=0;&nbsp; &nbsp; int sum1= intList.stream().reduce(ans, (x,y) -> x+y);&nbsp; &nbsp; System.out.println(sum1);&nbsp; &nbsp; int sum2= intList.stream().reduce(ans, Integer::sum);&nbsp; &nbsp; System.out.println(sum2);&nbsp; &nbsp; int max= intList.stream().max(Integer::compare).get();&nbsp; &nbsp; System.out.println(max);&nbsp; &nbsp; int min= intList.stream().min(Integer::compare).get();&nbsp; &nbsp; System.out.println(min);&nbsp; &nbsp; Double avg= intList.stream().collect(Collectors.averagingInt(x-> x));&nbsp; &nbsp; System.out.println(avg);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java