嗨,我正在制作一个应该采用字母等级并将其转换为GPA值的程序。程序的第一部分花了一个百分比并将其转换为字母等级,现在我正在尝试将转换后的字母等级转换为int值。我们只允许使用开关盒。但是,在执行此操作时,我发现了一个不兼容的类型错误:java.lang.String required:int。不知道我需要改变什么,有什么帮助吗?
谢谢
public static int convertToGradePoint(String letterGrade) {
int gradepoint;
switch (letterGrade) {
case "A+" :
gradepoint = 12;
break;
case "A" :
gradepoint = 11;
break;
case "A-" :
gradepoint = 10;
break;
case "B+" :
gradepoint = 9;
break;
case "B" :
gradepoint = 8;
break;
case "B-" :
gradepoint = 7;
break;
case "C+" :
gradepoint = 6;
break;
case "C" :
gradepoint = 5;
break;
case "C-" :
gradepoint = 4;
break;
case "D+" :
gradepoint = 3;
break;
case "D" :
gradepoint = 2;
break;
case "D-" :
gradepoint = 1;
break;
default :
gradepoint = 0;
break;
}
return gradepoint;
}
}
答案 0 :(得分:0)
您没有发布足够的代码。您可能正在执行以下操作:
String a = convertToGradePoint(input);
你需要这样做:
int a = convertToGradePoint(input);
答案 1 :(得分:0)
答案 2 :(得分:0)
除非你有Java7 +,否则你不能切换一个String实例,只能切换int(和byte / char / short,但不能切换long / double)。
替代方法是使用if如下
if(letterGrade.equals("A+")){
gradepoint = 12;
}
或升级您的Java