您如何找到最小值和最大值以查看它们是否匹配?但是,我正在寻找不是数组的整数的最小值和最大值。我需要找到平均值的最小值和最大值并进行比较。到目前为止,我有这个:

        int i;
        int totalall = 0;
        int total1 = 0;
        int total2 = 0;
        int total3 = 0;
        int total4 = 0;
        int min1 = Integer.MAX_VALUE, minIndex = 0;
        int max1 = Integer.MIN_VALUE, maxIndex = 0;
        int average1 = 0;
        int average2 = 0;
        int average3 = 0;
        int average4 = 0;

        System.out.print("Please enter the sample size: ");
        int max = input.nextInt();
        int[]arr0 = new int[max + 1];
        int[]arr1 = new int[max + 1];
        int[]arr2 = new int[max + 1];
        int[]arr3 = new int[max + 1];
        System.out.println("Enter numbers for Trial 0 ");
        for (i = 1;i <= max;i++){
            System.out.print("Enter sample #" + (i-1) + ":");
            arr0[i-1]= input.nextInt();
            total1 = total1 + arr0[i-1];
            }
        System.out.println("Enter numbers for Trial 1 ");
        for (i = 1; i <= max; i++){
            System.out.print("Enter sample #" + (i-1) + ":");
            arr1[i-1] = input.nextInt();
            total2 = total2 + arr1[i-1];
            }
        System.out.println("Enter numbers for Trial 2 ");
        for (i = 1; i <= max; i++){
            System.out.print("Enter sample #" + (i-1) + ":");
            arr2[i-1] = input.nextInt();
            total3 = total3 + arr2[i-1];
            }
        System.out.println("Enter numbers for Trial 3 ");
        for (i = 1; i <= max; i++){
            System.out.print("Enter sample #" + (i-1) + ":");
            arr3[i-1] = input.nextInt();
            total4 = total4 + arr3[i-1];
            }
        totalall += total1 + total2 + total3 + total4;
        average1 = total1 / (i-1);
        average2 = total2 / (i-1);
        average3 = total3 / (i-1);
        average4 = total4 / (i-1);

        System.out.println("\tSample #\tTrial 0\tTrial 1\tTrial 2\tTrial 3");
        System.out.println("\t\t" + arr3[max] + "\t" + arr0[i-2] + "\t" + arr1[i-2] + "\t" + arr2[i-2] + "\t" + arr3[i-2]);
        System.out.print("Average: \t\t");
        System.out.println(average1 + "\t" + average2 + "\t" + average3 + "\t" + average4);

        for (i=1; i < average; i++);


    }
}

最佳答案

也许这:

var averages = new List<int>(){average1,average2,average3,average4};
var maxAvg = averages.Max();
var minAvg = averages.Min();

10-06 06:34