简体   繁体   English

Java代码,查找浮点数的最小值,最大值和平均值

[英]java code, finding the min, max and average of floating point numbers

I wrote the below code everything works fine except it never executes the last loop so the last value inputted is never calculated into the min/max/average. 我写了下面的代码,一切正常,除非它从不执行最后一个循环,所以永远不会将输入的最后一个值计算为最小值/最大值/平均值。 Any idea where I went wrong? 知道我哪里出错了吗?

import java.util.Scanner;
public class Program4_JohnHuber {

public static void main(String[] args) {
    Scanner input = new Scanner (System.in);

   double total = 0;
   //double numbers = 0;
   int count = 1;
   double largest = 0;
   double smallest = Double.MAX_VALUE;
   double average = 0;

   System.out.print ("Please enter the grades, (Press enter when finished): ");
   {
       while (input.hasNextDouble()&& count<5)
       {
           double entered = input.nextDouble();
           total = total + entered;
           //count++;

       //double average = 0;
       if (count > 0)
       {
           average = total/count;
       }
       if (entered > largest)
       {
           largest = entered;
       }
       if (entered < smallest)
       {
           smallest = entered;
       }
       count++;
       }

   }
       System.out.printf ("Your average grade is %3.2f,\n", average);
       System.out.printf ("Your highest grade is %3.2f \n", largest);
       System.out.printf ("Your lowest grade is %3.2f \n", smallest);



}
}

There are two errors in your program (assuming your intent is to input 5 numbers): 程序中有两个错误(假设您打算输入5个数字):

  1. You're using count to indicate the number of grades you've entered already. 您使用count来表示您已经输入的成绩数。 Before you've entered any grades, how many grades have you entered? 输入任何等级之前,您输入了多少个等级? That's the value count should have, but you've initialized it to the wrong value. 那就是值count ,但是您已将其初始化为错误的值。
  2. The other issue is in how you've written the while : 另一个问题是您编写while

      while (input.hasNextDouble() && count<5) 

Suppose you've fixed the first problem, so that it now lets you enter 5 numbers and keeps statistics on those numbers. 假设您已经解决了第一个问题,那么现在可以输入5个数字并保留这些数字的统计信息。 Now it goes back up to the while loop and evaluates the boolean expression. 现在,它返回到while循环并评估布尔表达式。

At this point, count is 5, so you want to exit the loop. 此时, count为5,因此您想退出循环。 But that doesn't happen, because input.hasNextDouble() is evaluated first . 但这不会发生,因为input.hasNextDouble() 首先被评估。 Since you're using a scanner on System.in , that means that the program waits until either you type in something that isn't blank, or until you indicate the end of input with CTRL+D on Linux or CTRL+Z on Windows. 由于您在System.in上使用扫描仪,因此该程序将一直等到您键入非空白的内容,或者直到您在Linux上使用CTRL + D或在Windows上使用CTRL + Z指示输入结束。 After it finds the next item in the input, it will then exit the loop if it can't find a double (eg you type in some letters); 输入中找到下一项后,如果找不到double (例如,您输入一些字母),它将退出循环。 or if you put in a double , then it checks count . 或者,如果你把一个double那么它会检查count

The combination of these two errors is why the program appears to be ignoring the last input: (1) it only does the computation on 4 grades, not 5, because of the error in initializing count , and (2) it asks for a 5th grade anyway, because the parts of the while loop condition are in the wrong order. 这两个错误的结合是程序似乎忽略最后输入的原因:(1)由于初始化count错误,它仅对4个等级而不是5进行count ,并且(2)要求第5个等级无论如何还是要进行分级,因为while循环条件的各个部分顺序错误。

To fix the second problem, change it to 要解决第二个问题,请将其更改为

while (count < 5 && input.hasNextDouble())

This checks count first , and exits the loop immediately when you have enough grades, instead of looking for more input. 该检查首先检查count ,并且在您有足够的分数时立即退出循环,而不是寻找更多输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM