用户输入后在Java中显示最低和更高的数字?

时间:2013-11-12 19:25:53

标签: java

如何在Java中显示最低和最高的数字?我知道我需要使用这个变量:int max = Integer.MIN_VALUE; int min = Integer.MIN_VALUE;但我无法让它发挥作用。 :/任何帮助?

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


   }
  }

1 个答案:

答案 0 :(得分:0)

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

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

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

if (min >= temp) 
{ 
 min = temp;
} 
if (max <= temp) 
{ 
max = temp;
}