Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一个带有分数(92)的txt文件被分配了所有A';s、 但分数(92 10 70 95 84)92和95被分配';B';?_Java_Arrays_Debugging_Methods - Fatal编程技术网

Java 一个带有分数(92)的txt文件被分配了所有A';s、 但分数(92 10 70 95 84)92和95被分配';B';?

Java 一个带有分数(92)的txt文件被分配了所有A';s、 但分数(92 10 70 95 84)92和95被分配';B';?,java,arrays,debugging,methods,Java,Arrays,Debugging,Methods,所以我需要帮助调试我的代码。我的代码的问题是数组中的每个分数都分配了不正确的分数。我的文件读取一个txt文件,其中包括名字[space]姓氏[tab]分数。然后将它们存储在数组中,计算分数的平均值和标准偏差,然后为分数分配字母等级,但问题是如果我将txt文件重写为(92 92),它们都被分配为“A”,这是正确的,但是当txt文件是(92 10 70 95 84)时,92和95被分配为“B”,而不是“A”,这是唯一的问题。我花了几个小时试图找出它为什么会这样做,但没有运气。他正在学习如何计算字母等

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


A=平均值+标准对于第二种情况,平均值为52.2,标准差为~40。对于92,
(mean+(standard/3)我建议的一个注释是,如果你使用
else-if
s来表示其他字母,而不是使用
if
s,你可以去掉次要条件并覆盖值。条件
B=mean+(standard/3)不是吗
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;
    }  
}