简体   繁体   中英

How do you find the minimum and maximum to see if they match?

How do you find the minimum and maximum to see if they match? But the thing I'm finding the minimum and maximum for integers that are not an array. I need to find the minimum and maximum of the averages and compare them. So far I have this:

        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++);


    }
}

Perhaps this:

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

do you need this?

import java.util.Scanner;

public class test {

    public static void main(String[] args) {
        int max = 0;
        int min = 0;
        int total = 0;
        int avg = 0;
        float count = 0;
        String input = "";

        Scanner scanner = new Scanner(System.in);
        while (!(input = scanner.nextLine()).equals("end")) {


            if(max < Integer.parseInt(input))
                max = Integer.parseInt(input);
            else if(min > Integer.parseInt(input))
                min = Integer.parseInt(input);
            if(count == 0)
                max = min = Integer.parseInt(input);
            total += Integer.parseInt(input);
            count++;
        }
        System.out.println("max: " + max);
        System.out.println("min: " + min);
        System.out.println("total: " + total);
        System.out.println("avg: " + total / count);        
    }

}

To find the minimum of a list of variables (which are not an array) you can let java syntax help you: The NumberUtils class comes from Apache Commons-Lang library

public class ArrayTest {

    public static int min(int ... value)
    {
        return org.apache.commons.lang.math.NumberUtils.min(value);
    }
    public static int max(int ... value)
    {
        return org.apache.commons.lang.math.NumberUtils.max(value);
    }

    public static void main(String[] args) {
        int average1=4, average2=20, average3=40;
        System.out.println("minimum:"+min(average1,average2,average3));
        System.out.println("maximum:"+max(average1,average2,average3));
    }
}

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