如何找到最小值和最大值以查看它们是否匹配?但是我发现不是数组的整数的最小值和最大值。我需要找到平均值的最小值和最大值并进行比较。到目前为止,我有这个:
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++);
}
}
答案 0 :(得分:1)
也许这就是:
var averages = new List<int>(){average1,average2,average3,average4};
var maxAvg = averages.Max();
var minAvg = averages.Min();
答案 1 :(得分:0)
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);
}
}
答案 2 :(得分:0)
要查找变量列表(不是数组)的最小值,您可以让java语法帮助您: NumberUtils类来自Apache Commons-Lang库
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));
}
}