嗨,我对以下程序有疑问,因为它给了我一个错误说,variable gradePoints might not have been initialized
我在这里缺少什么..?另外我该如何改进代码,以便它接受字母 a+ 作为 A+ 并且任何其他输入都会引发错误?谢谢!!
import java.lang.*;
import java.io.*;
import java.util.*;
import java.util.Scanner;
public class Program
{
static final Map<String, Double> gradeToPointMap =
new LinkedHashMap<String, Double>() {{
put("A+", 4.0);
put("A", 4.0);
put("A-", 3.7);
put("B+", 3.3);
put("B", 3.0);
put("B-", 2.7);
put("C+", 2.3);
put("C", 2.0);
put("C-", 1.7);
put("F", 0.0);
}};
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter A Letter Grade: ");
String letterGrade = keyboard.next();
getGradePoint(letterGrade);
}
public double getGradePoint(String letterGrade)
{
Double gradePoints = gradeToPointMap.get(letterGrade.toUpperCase());
if (gradePoints == null)
System.out.println("Unknown letter grade " + letterGrade);
else
System.out.println("Your grade point(GPA) is " + gradePoints);
}
}