2

我正在尝试结合从用户输入中获取最小值和最大值的功能,但似乎无法使其正常工作。我尝试了一个while循环,但不确定如何真正存储最小值和最大值。

public class examReview
{
    public static void main (String[]args)
    {
        Scanner input = new Scanner(System.in);
        int numOfInputs=0;
        int currentMax
        int currentMin=0;
        double sum=0;
        int intInput = 1;
        int num;

        while (intInput != 0)
        {
            intInput = input.nextInt();

            currentMin = intInput;
            currentMax = intInput;
            System.out.println("currentminis" + currentMin);
            System.out.println("currentmaxis" + currentMax);

            sum += intInput;
            numOfInputs++;
        }

        System.out.println(numOfInputs - 1);     //Prints number of input
        System.out.println(sum);                 //Prints sum of all values entered
        System.out.println(sum/(numOfInputs-1)); //Prints average
        System.out.println(currentMin);
    }
}
4

5 回答 5

0
while (intInput != 0)
  {
     intInput = input.nextInt();

     if(numOfInputs==0 || intInput<currentMin)currentMin = intInput;
     if(numOfInputs==0 || intInput>currentMax)currentMax = intInput;
     System.out.println("currentminis" + currentMin);
     System.out.println("currentmaxis" + currentMax);

     sum += intInput;
     numOfInputs++;

  }
于 2013-09-25T16:07:04.410 回答
0

您当前在 while 循环中每次都 更改currentMinand 。 显然,这些需要在循环之外设置。currentMax

   int currentMax=Integer.MIN_VALUE; 
   int currentMin=Interger.MAX_VALUE;

并在while循环内根据需要进行调整

    while (intInput != 0)
    {
        intInput = input.nextInt();

        if(intInput<currentMin) currentMin = intInput;
        if(intInput>currentMax) currentMax = intInput;
        System.out.println("currentminis" + currentMin);
        System.out.println("currentmaxis" + currentMax);

        sum += intInput;
        numOfInputs++;
    }
于 2013-09-25T16:17:27.710 回答
0

汤姆有正确的答案,您想针对 intInput 测试您的当前{Min,Max}:

if (intInput < currentMin ) {
      currentMin = intInput;
}

您可以通过将 currentMin 和 currentMax 分配给异常值来简化标志日志,并且只要您获得至少一个输入,这已经导致问题,因为您的平均值将被零除,您应该没问题。

int currentMax = Integer.MIN_VALUE;
int currentMin = Integer.MAX_VALUE;

这样,任何值都将大于 currentMax 并小于 currentMin,您可以摆脱该标志。

另请注意,平均值可能应该是sum / numOfInputs而不是sum / numOfInputs-1

于 2013-09-25T16:18:31.253 回答
0

我觉得你写的代码太多了。您应该使用 java API:

尝试这个:

public static void main (String[]args) {
    Scanner input = new Scanner(System.in);
    List<Integer> list = new ArrayList<Integer>();
    int sum = 0;
    for (int i = input.nextInt(); i > 0; i = input.nextInt()) {
        sum += i;
        list.add(i);
    }

    System.out.println(list.size());          //Prints number of input
    System.out.println(sum);                  //Prints sum of all values entered
    System.out.println(list.isEmpty() ? 0d ; sum/list.size()); //Prints average
    System.out.println(Collections.min(list));//Prints min
    System.out.println(Collections.max(list));//Prints max
}
于 2013-09-25T16:19:41.640 回答
0

您需要将最大值设置为最小值,将最小值设置为最大值。然后在需要的时候比较和设置:

int currentMax = Integer.MIN_VALUE;
int currentMin = Integer.MAX_VALUE;

while(haveInput) {
   if (intInput > currentMax) currentMax = intInput;
   if (intInput < currentMin) currentMin = intInput;
   // get more input
}
于 2013-09-25T16:20:32.257 回答