簡體   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