代码之家  ›  专栏  ›  技术社区  ›  Mark Yo

从成绩转换为gpa(带小数)[重复]

  •  1
  • Mark Yo  · 技术社区  · 6 年前

    我有以下代码:

    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;
            }
        }
    }
    

    我的目标是转换grades数组 {97, 64, 75, 100, 21} 到一个新的GPA数组,该数组将分数转换为4.0、3.9或其他值。我遇到了以下错误:

    线程“main”java中出现异常。lang.NullPointerException在家庭作业中7。主要的

    如何解决此问题?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Aaron Stein    6 年前

    您可能没有初始化 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;
            }
         }
    }