You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

成绩转带小数GPA的Java代码实现问题咨询

Hey there! Let's get your GPA conversion code fully functional so it handles every score in your array properly. Here's how to fix it up step by step:

Step 1: Fix Initialization & Basic Setup

First off, your stuGpa array isn't initialized yet—this will cause a NullPointerException when you try to assign values to it. We need to create a double array with the same dimensions as your stuGrades array, since GPA values are decimals.

Also, since your scores are stored as integers, comparing them to decimal thresholds (like 98.1) is a bit awkward. We can adjust the conditions to use integer ranges instead for clarity and accuracy.

Step 2: Complete the Score-to-GPA Mapping Logic

Your existing code only handles the top two score ranges. We need to fill in all the intermediate ranges, plus the failing range (scores below 60). I've extended the pattern from your original code—each 2-point drop (mostly) corresponds to a 0.1 GPA drop, down to a 2.0 for a 60. Scores below 60 get a 0.0 (you can adjust this if your school uses a different failing GPA rule).

Full Working Code
public class GpaConverter {
    public static void main(String[] args) {
        int[][] stuGrades = {{97, 64, 75, 100, 21}}; // From data file
        // Initialize GPA array with matching dimensions
        double[][] stuGpa = new double[stuGrades.length][stuGrades[0].length];

        for (int g = 0; g < stuGrades.length; g++) {
            for (int p = 0; p < stuGrades[g].length; p++) {
                int tempScores = stuGrades[g][p];
                if (tempScores >= 99 && tempScores <= 100) {
                    stuGpa[g][p] = 4.0;
                } else if (tempScores >= 97 && tempScores <= 98) {
                    stuGpa[g][p] = 3.9;
                } else if (tempScores >= 95 && tempScores <= 96) {
                    stuGpa[g][p] = 3.8;
                } else if (tempScores >= 93 && tempScores <= 94) {
                    stuGpa[g][p] = 3.7;
                } else if (tempScores >= 91 && tempScores <= 92) {
                    stuGpa[g][p] = 3.6;
                } else if (tempScores >= 89 && tempScores <= 90) {
                    stuGpa[g][p] = 3.5;
                } else if (tempScores >= 87 && tempScores <= 88) {
                    stuGpa[g][p] = 3.4;
                } else if (tempScores >= 85 && tempScores <= 86) {
                    stuGpa[g][p] = 3.3;
                } else if (tempScores >= 83 && tempScores <= 84) {
                    stuGpa[g][p] = 3.2;
                } else if (tempScores >= 81 && tempScores <= 82) {
                    stuGpa[g][p] = 3.1;
                } else if (tempScores >= 79 && tempScores <= 80) {
                    stuGpa[g][p] = 3.0;
                } else if (tempScores >= 77 && tempScores <= 78) {
                    stuGpa[g][p] = 2.9;
                } else if (tempScores >= 75 && tempScores <= 76) {
                    stuGpa[g][p] = 2.8;
                } else if (tempScores >= 73 && tempScores <= 74) {
                    stuGpa[g][p] = 2.7;
                } else if (tempScores >= 71 && tempScores <= 72) {
                    stuGpa[g][p] = 2.6;
                } else if (tempScores >= 69 && tempScores <= 70) {
                    stuGpa[g][p] = 2.5;
                } else if (tempScores >= 67 && tempScores <= 68) {
                    stuGpa[g][p] = 2.4;
                } else if (tempScores >= 65 && tempScores <= 66) {
                    stuGpa[g][p] = 2.3;
                } else if (tempScores >= 63 && tempScores <= 64) {
                    stuGpa[g][p] = 2.2;
                } else if (tempScores >= 61 && tempScores <= 62) {
                    stuGpa[g][p] = 2.1;
                } else if (tempScores == 60) {
                    stuGpa[g][p] = 2.0;
                } else if (tempScores < 60 && tempScores >= 0) {
                    stuGpa[g][p] = 0.0;
                } else {
                    // Handle invalid scores (negative or over 100)
                    stuGpa[g][p] = -1.0; // Mark as invalid
                }
            }
        }

        // Print the result to verify
        for (double[] gpaRow : stuGpa) {
            for (double gpa : gpaRow) {
                System.out.print(gpa + " ");
            }
        }
    }
}
Test Result for Your Input Array

When you run this code with your input {97, 64, 75, 100, 21}, the output will be:

3.9 2.2 2.8 4.0 0.0

This matches the full conversion you need.

Notes
  • If your school uses a different GPA scale (e.g., failing scores get 1.0 instead of 0.0), just adjust the corresponding else if block.
  • The final else clause catches invalid scores (like negatives or values over 100) and marks them with -1.0 so you can spot data issues easily.

内容的提问来源于stack exchange,提问作者Mark Yo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:45:23