如何在Java中显示最小和最大的数字?我知道我需要使用这个变量: int max = Integer.MIN_VALUE; int min = Integer.MIN_VALUE; 但我无法让它工作。:/有什么帮助吗?
导入 java.util.Scanner;
public class LoopingNumbersUsingWhile {
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);
System.out.println("How Many Numbers You Want To Enter: ");
int total = kb.nextInt();
int input = 0;
int sum = 0;
int average=0;
int max = Integer.MIN_VALUE;
int min = Integer.MIN_VALUE;
while (input < total) {
input++;
System.out.println("Enter " + input + ". Operand: ");
sum += kb.nextInt();
if (min >= sum)
{
min = sum;
}
if (max <= sum)
{
max = sum;
}
average = ( sum ) / ( input);
}
System.out.println("The sum is " + sum + ".");
System.out.println("The avg " + average);
System.out.println("The higher number " + max);
System.out.println("The the lowest number " + min);
}
}