有人可以帮助解释为什么我无法显示正确的中位数吗? 【JAVA]

时间:2021-10-04 23:08:18

so the program I am making is supposed to be able to determine the mean, median and how many "grades" have been put into the program. trying to get the median to display and not having any errors using I guess a popular way of finding the median (google thinks its a good idea of how to find the median). When I put in a number I always get 0.0 until I max out my array (set to 25) and then it becomes 1.0. Still not really doing anything math that its supposed to be doing.

所以我正在制作的程序应该能够确定平均值,中位数以及已经将多少“等级”放入程序中。试图获得中位数显示并没有任何错误使用我猜一种流行的方式找到中位数(谷歌认为它是如何找到中位数的好主意)。当我输入一个数字时,我总是得到0.0,直到我最大化我的数组(设置为25)然后它变为1.0。仍然没有真正做任何数学应该做的事情。

  DecimalFormat df = new DecimalFormat ("#0.0##");
   String textBoxInputStr;
   String gCountStr;
   double boxInput = 0;
   double[]gradeArray = new double[25];
   int gradeCount = 0;
   double mean = 0;
   String meanStr;
   double sum = 0;
   double median = 0;
   String medianStr;
   do {    
      try {
         if(gradeCount < 25) {
             textBoxInputStr = JOptionPane.showInputDialog(this,"Enter Grade","Enter Grade", JOptionPane.PLAIN_MESSAGE);  //makes texbox

             boxInput = Double.parseDouble(textBoxInputStr);                //parses what goes on in the textbox

             gradeArray[gradeCount] = boxInput;                           //puts the parsed data into the array 
             gradeCount++;                                                //is the tracker for how many grades are entered
             gCountStr = Integer.toString(gradeCount);                    //converts the counter to string
             txtGrades.setText(gCountStr);                                //places the newly converted counter into grades box for display
          }
         else      //an error
             {      
            JOptionPane.showInputDialog(this,"You can only input 25 values","Too Much Data", JOptionPane.PLAIN_MESSAGE); 
             }
     //(Above) this is all to keep track of how many grades are entered and all the grades are stored in the array.
     //start of median stuff
     Arrays.sort(gradeArray, 0, gradeCount);
  if (gradeCount % 2 == 0) {
      median = ((double)gradeArray[gradeCount/2] + (double)gradeArray[gradeCount/2 - 1])/2;
     medianStr = Double.toString(median);
      txtMedian.setText(medianStr);
  }
  else {
       median = (double) gradeArray[gradeCount/2];
       medianStr = Double.toString(median);
      txtMedian.setText(medianStr);
  }
         //start of the average
               for (double i : gradeArray){
                   sum += i;
               }
               mean = sum / gradeCount;
               meanStr = Double.toString(mean);
               txtMean.setText(meanStr);

       }
     catch(NumberFormatException | HeadlessException e)  //catches bad data
                  {
                  JOptionPane.showMessageDialog(this, "Your input must be numeric!", "Bad Data!", JOptionPane.ERROR_MESSAGE);
                  }
     }
     while(gradeCount <= 25);

      }                                       

1 个解决方案

#1


2  

Two problems.

First, your do-while loop is missing braces to denote the body. (is this in your original code? or only what you posted here?)

首先,你的do-while循环缺少括号来表示正文。 (这是你的原始代码吗?或者只是你在这里发布的内容?)

Second, you are declaring gradeArray as a primitive array, but using its length to calculate an updated median every iteration. The length of a primitive array never changes from the time it is instantiated--it is not dependent on how many elements have been added. In other words, gradeArray.length is always 25 (in your example), regardless of how many indices you have actually populated with grades.

其次,您将gradeArray声明为基本数组,但使用其长度计算每次迭代的更新中值。原始数组的长度从实例化时起不会改变 - 它不依赖于添加了多少元素。换句话说,gradeArray.length总是25(在您的示例中),无论您实际使用等级填充了多少个索引。

To solve the first problem, just add braces around everything after do and before while.

要解决第一个问题,只需在do之后和之前添加大括号。

To solve the second problem, you should calculate the median using gradeCount as the "length" of your array, rather than the full (static) length of the primitive array.

要解决第二个问题,您应该使用gradeCount作为数组的“长度”来计算中值,而不是原始数组的完整(静态)长度。


EDIT:

...also, I just noticed that your sort is over the full array (so all 25 indices). This may also be causing problems, since the it will consider the "empty" indices (which actually have a default value of 0.0) as part of the elements to sort. Solve this problem in a similar way. Only sort the portion of the array that actually has grades you have entered--i.e. use this method signature instead. E.g.

...而且,我只是注意到你的排序已经超过了完整的数组(因此所有25个索引)。这也可能导致问题,因为它会将“空”索引(实际上具有默认值0.0)视为要排序的元素的一部分。以类似的方式解决这个问题。只对数组中实际具有您输入成绩的部分进行排序 - 即。请改用此方法签名。例如。

Array.sort(gradeArray, 0, gradeCount);

SECOND EDIT:

A third problem (that I think is unrelated, but still important to fix) is that the line if (boxInput >= 0) should instead be if (gradeCount < 25). ...assuming that this line is intended to do what the comment says it does: "makes the check for how many grades are entered".

第三个问题(我认为不相关,但仍然很重要)是if(boxInput> = 0)行应该是if(gradeCount <25)。 ......假设这一行是为了做评论所说的那样:“检查输入的等级数”。

#1


2  

Two problems.

First, your do-while loop is missing braces to denote the body. (is this in your original code? or only what you posted here?)

首先,你的do-while循环缺少括号来表示正文。 (这是你的原始代码吗?或者只是你在这里发布的内容?)

Second, you are declaring gradeArray as a primitive array, but using its length to calculate an updated median every iteration. The length of a primitive array never changes from the time it is instantiated--it is not dependent on how many elements have been added. In other words, gradeArray.length is always 25 (in your example), regardless of how many indices you have actually populated with grades.

其次,您将gradeArray声明为基本数组,但使用其长度计算每次迭代的更新中值。原始数组的长度从实例化时起不会改变 - 它不依赖于添加了多少元素。换句话说,gradeArray.length总是25(在您的示例中),无论您实际使用等级填充了多少个索引。

To solve the first problem, just add braces around everything after do and before while.

要解决第一个问题,只需在do之后和之前添加大括号。

To solve the second problem, you should calculate the median using gradeCount as the "length" of your array, rather than the full (static) length of the primitive array.

要解决第二个问题,您应该使用gradeCount作为数组的“长度”来计算中值,而不是原始数组的完整(静态)长度。


EDIT:

...also, I just noticed that your sort is over the full array (so all 25 indices). This may also be causing problems, since the it will consider the "empty" indices (which actually have a default value of 0.0) as part of the elements to sort. Solve this problem in a similar way. Only sort the portion of the array that actually has grades you have entered--i.e. use this method signature instead. E.g.

...而且,我只是注意到你的排序已经超过了完整的数组(因此所有25个索引)。这也可能导致问题,因为它会将“空”索引(实际上具有默认值0.0)视为要排序的元素的一部分。以类似的方式解决这个问题。只对数组中实际具有您输入成绩的部分进行排序 - 即。请改用此方法签名。例如。

Array.sort(gradeArray, 0, gradeCount);

SECOND EDIT:

A third problem (that I think is unrelated, but still important to fix) is that the line if (boxInput >= 0) should instead be if (gradeCount < 25). ...assuming that this line is intended to do what the comment says it does: "makes the check for how many grades are entered".

第三个问题(我认为不相关,但仍然很重要)是if(boxInput> = 0)行应该是if(gradeCount <25)。 ......假设这一行是为了做评论所说的那样:“检查输入的等级数”。