繁体   English   中英

使用 for 循环查找 n 个值的最大值、最小值、总和和平均值

[英]Finding max, min, sum, and average of n amount of values using for loop

使用for循环编写一个程序,提示用户输入12个n个数字,然后显示这些数字的最小值、最大值、总和和平均值。

我不明白我将如何使用 for 循环来实现这一点,我遇到了一些麻烦。

import java.util.Scanner;

public class Loop{

    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter number 1: ");
        double a = scanner.nextDouble();

        System.out.println("Enter number 2: ");
        double b = scanner.nextDouble();

        System.out.println("Enter number 3: ");
        double c = scanner.nextDouble();

        System.out.println("Enter number 4: ");
        double d = scanner.nextDouble();

        System.out.println("Enter number 5: ");
        double e = scanner.nextDouble();    

        System.out.println("Enter number 6: ");
        double f = scanner.nextDouble();    

        System.out.println("Enter number 7: ");
        double g = scanner.nextDouble();    

        System.out.println("Enter number 8: ");
        double h = scanner.nextDouble();    

        System.out.println("Enter number 9: ");
        double i = scanner.nextDouble();

        System.out.println("Enter number 10: ");
        double j = scanner.nextDouble();    

        System.out.println("Enter number 11: ");
        double k = scanner.nextDouble();    

        System.out.println("Enter number 12: ");
        double l = scanner.nextDouble();    

        double minNum = Math.min(a, Math.min(b, Math.min(c, Math.min(d, Math.min(e, Math.min(f, Math.min(g, Math.min(h, Math.min(i , Math.min(j, Math.min(k, l)))))))))));
        double maxNum = Math.max(a, Math.max(b, Math.max(c, Math.max(d, Math.max(e, Math.max(f, Math.max(g, Math.max(h, Math.max(i , Math.max(j, Math.max(k, l)))))))))));
        double sumNum = (a + b + c + d + e + f + g + h + i + j + k + l);

        for(int i = 0; )


    }

}

我希望输出与 for 循环一起使用,但是我又不明白我将如何在这种方法中使用它。

这是一个更好的方法,它甚至不需要一次存储所有数字。 这里的方法是在输入每个新数字时在最大和最小输入上保留一个正在运行的选项卡。 此外,我们维护所有输入数字的运行总数,稍后可以轻松地用于计算平均值。

Scanner scanner = new Scanner(System.in);
double max, min, avg;
double total = 0.0d;
boolean first = true;
int numbers = 12;

for (int i=0; i < numbers; ++i) {
    System.out.println("Enter number:");
    double d = scanner.nextDouble();

    if (first) {
        max = d;
        min = d;
        total = d;
        first = false;
    }
    else {
        if (d > max) {
            max = d;
        }
        else if (d < min) {
            min = d;
        }
        total += d;
    }
}

avg = total / numbers;
System.out.println("max: " + max + ", min: " + min + ", avg: " + avg);

请注意,更高级的方法可能是将所有 12 个数字存储到一个列表中,然后利用 Java 8 流 API 来查找最大值、最小值和平均值。 但我怀疑这不是您想要的问题答案。

将此java代码放在main函数中

`Scanner s=new Scanner(System.in);
int t=s.nextInt(); //12
int sum=0;
int min=Integer.MIN_VALUE;
int max=Integer.MAX_VALUE;
for(int i=0;i<t;i++){
int temp=s.nextInt();
if(temp<min)
 min=temp;
if(temp>max)
 max=temp;
sum+=temp;
}
System.out.println(min +" "+max+" "+sum);`

你可以这样做。

Scanner scanner = new Scanner(System.in);
double max = Double.MAX_VALUE, min = Double.MIN_VALUE, total = 0.00d;
int n = 12; // Change this to the number of values needed
for (int i=0; i < n; i++) {
    System.out.println("Enter number:");
    double d = scanner.nextDouble();

    if (d > max) {
        max = d;
    }
    else if (d < min) {
        min = d;
    }
    total += d;
}

Double avg = total / n;
System.out.println("max: " + max + ", min: " + min + ", avg: " + avg);

暂无
暂无

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

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