从成绩转换为gpa(带小数)

时间:2018-05-06 16:12:39

标签: java arrays 2d

我有这段代码:

int[][] stuGrades = {{97, 64, 75, 100, 21}}; //extract from data file

String[][] HWdata; // original data file
for (int g = 0; g < stuGrades.length; g++) {
    for (int p = 0; p < stuGrades[0].length; p++) {
        int tempScores = stuGrades[g][p];
        if (tempScores <= 100 && tempScores > 98.1) {
            stuGpa[g][p] = 4.0;
        }
        else if (tempScores <= 98 && tempScores > 96.1) {
            stuGpa[g][p] = 3.9;
        }
    }
}

我的目标是将成绩数组{97, 64, 75, 100, 21}转换为新的GPA数组,该数组会将得分转换为4.0,3.9或其他内容。我收到了这个错误:

  

homework7.main中线程“main”java.lang.NullPointerException中的异常。

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

你可能没有正确地初始化stuGpa,正如MalumAtire832指出的那样。

int[][] stuGrades = {{97, 64, 75, 100, 21}}; //extract from data file

double[][] stuGpa = new double[stuGrades.length][stuGrades[0].length];

String[][] HWdata; // original data file
for (int g = 0; g < stuGrades.length; g++) {
    for (int p = 0; p < stuGrades[0].length; p++) {
        int tempScores = stuGrades[g][p];
        if (tempScores <= 100 && tempScores > 98.1) {
            stuGpa[g][p] = 4.0;
        } else if (tempScores <= 98 && tempScores > 96.1) {
            stuGpa[g][p] = 3.9;
        }
     }
}