得分(92 92 92 92 92)的txt文件被分配所有A,但得分(92 10 70 95 84)92和95被分配'B'?

时间:2017-01-17 01:19:28

标签: java arrays debugging methods

所以我需要帮助调试我的代码。我的代码的问题是我的数组中的每个分数都分配了不正确的分数。我的文件读入一个包含名字[空格]姓氏[tab]得分的txt文件。然后它将它们存储在数组中,计算得分的平均值和标准差,然后应该为得分分配字​​母等级,但问题是,如果我将txt文件重写为(92 92 92 92 92),则它们全部被分配' ''是正确的,但是当txt文件是(92 10 70 95 84)时,92和95被分配'B'而不是'A',这是唯一的问题。我花了几个小时试图找出它为什么这样做,但没有运气。他是如何计算字母等级的:

A =平均值+标准< =得分,B =平均值+(标准/ 3)< =得分<平均值+标准,C =平均值 - (标准/ 3)< =得分<平均值+(标准/ 3),D =平均值 - 标准< =得分<平均值 - (标准/ 3),F =得分<平均值 - 标准

import java.io.File;
import java.io.FileNotFoundException;    
import java.util.Scanner;

public class ScannerReadFileSplit {

public static int[] scores = new int[5];
public static String[] names = new String[5];
public static char[] grades = new char[5];
public static int mean = 0;
public static double standard = 0.0;

public static void main(String[] args) {


    readData();       
    mean = fndMean();
    standard = fndStandard();

    for(int x = 0; x < scores.length; x++) {
      if(mean + standard <= scores[x]) {
        grades[x] = 'A';

      } if( (mean + (standard/3) <= scores[x]) && (scores[x] < mean + standard)) {
        grades[x] = 'B';

      } if( (mean - (standard/3) <= scores[x]) && (scores[x] < mean+(standard/3))) {
        grades[x] = 'C';

      } if( (mean - standard <= scores[x]) && (scores[x] < mean - (standard/3))) {
        grades[x] = 'D';

      }
      if(scores[x] < mean - standard) {
        grades[x] = 'F';
      }
    }

    System.out.printf("%-22s%-22s%-22s\n", "Names", "Scores", "Grade");
        for(int k=0; k<5; k++) {
            System.out.printf("%-22s%-22d%-22c\n", names[k], scores[k], grades[k]);
           }
           System.out.println();
}


  //Method called readData() that reads input from the file and stores the names
  //in an array called names and scores in an array called scores. * Each names has
  //to be a seperate line and scores have to be seperated by a tab.
  public static void readData() {

    File file = new File("NamesScore.txt");
    int i = 0;

    try {

     Scanner scanner = new Scanner(file);
        while (scanner.hasNextLine()) {

            String line = scanner.nextLine();
            String [] words = line.split("\t");

            names[i] = words[0];
            scores[i] = Integer.parseInt(words[1]);
            i++;
          }

        } catch (FileNotFoundException e) {
          e.printStackTrace();
          System.exit(0);
      }
  }

  //Method called fndMean() that goes through the scores array, adds all elements
  //and then divides that sum by the number of elements, which then stores that value
  //in a variable mean1 then returns that value.
  public static int fndMean() {
        int mean1 = 0;
        int sum = 0;
        for(int j = 0; j < scores.length; j++) {
            sum += scores[j];
        }
        mean1 = (sum/(scores.length));
        System.out.printf("The mean of the scores is: %d\n", mean1);
        return mean1;
    }

  //Method called fndStandard() that finds the standard deviation of the scores by using
  //the following formula. That value is then stores in a variable standardDeviation1 then
  //returns that value 
  public static double fndStandard() {
         double sd = 0;
         for(int i = 0; i < scores.length; i++) {
             sd += ((scores[i] - mean) * (scores[i] - mean)) / (scores.length - 1);
         }
         double standardDeviation1 = Math.sqrt(sd);
         System.out.printf("The standard deviation is: %.2f\n", standardDeviation1);
         return standardDeviation1;
    }  
}

1 个答案:

答案 0 :(得分:0)

对于第二种情况,您的平均值为52.2,标准值为~40。对于92,(mean + (standard/3) <= scores[x]) && (scores[x] < mean + standard))计算为52.2 + 40/3 <92和92 <52.2 + 40以分配B,因此它看起来是正确的。