0

如何在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);


   }
  }
4

1 回答 1

0

您需要将下一个数字读入临时变量,然后将其添加到 sum。

int temp = kb.nextInt();
sum+=temp

并对您的临时变量进行这些检查。

if (min >= temp) 
{ 
 min = temp;
} 
if (max <= temp) 
{ 
max = temp;
} 
于 2013-11-12T19:28:38.817 回答