我在最后一个括号上得到一个丢失的return语句错误,我不明白为什么。该方法基本上采用与学生课程相对应的字符串以及他们所处的当前调查问题。外部if语句对应于主题(例如:数学,科学,英语......)。 for循环遍历数组并将用户输入(" US History")与数组列表中的字符串进行匹配。如果它们相等,则返回找到它的索引。否则,它继续for循环。该方法返回一个int,但我仍然在最后一个括号上得到一个错误。请帮助!
public static int findNumber(String course, int currentQuestion) {
String newCourse = course.toLowerCase();
if (RateDriver.levelText.equals("math")) {
for (int i = 0; i < RateSurvey.math.length-1;i++) {
if (newCourse.equals(RateSurvey.math[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("science")) {
for (int i = 0; i < RateSurvey.science.length-1;i++) {
if (newCourse.equals(RateSurvey.science[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("english")) {
for (int i = 0; i < RateSurvey.english.length-1;i++) {
if (newCourse.equals(RateSurvey.english[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("social studies")) {
for (int i = 0; i < RateSurvey.social.length-1;i++) {
if (newCourse.equals(RateSurvey.social[i])) return survey[currentQuestion][i];
}
}
else if (RateDriver.levelText.equals("art")) {
for (int i = 0; i < RateSurvey.art.length-1;i++) {
if (newCourse.equals(RateSurvey.art[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("pe")) {
for (int i = 0; i < RateSurvey.pe.length-1;i++) {
if (newCourse.equals(RateSurvey.pe[i])) return survey[currentQuestion][i];
}
}
else if (RateDriver.levelText.equals("chinese")) {
for (int i = 0; i < RateSurvey.chinese.length-1;i++) {
if (newCourse.equals(RateSurvey.chinese[i])) return survey[currentQuestion][i];
}
}
else if (RateDriver.levelText.equals("japanese")) {
for (int i = 0; i < RateSurvey.japanese.length-1;i++) {
if (newCourse.equals(RateSurvey.japanese[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("french")) {
for (int i = 0; i < RateSurvey.french.length-1;i++) {
if (newCourse.equals(RateSurvey.french[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("spanish")) {
for (int i = 0; i < RateSurvey.spanish.length-1;i++) {
if (newCourse.equals(RateSurvey.spanish[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("hawaiian")) {
for (int i = 0; i < RateSurvey.hawaiian.length-1;i++) {
if (newCourse.equals(RateSurvey.hawaiian[i])) return survey[currentQuestion][i];
}
return 100;
}
else return 100;
**}** // this is where I get the error
答案 0 :(得分:2)
如果for循环中没有返回任何内容,代码的某些部分不会返回任何内容,例如:
else if (RateDriver.levelText.equals("social studies")) {
for (int i = 0; i < RateSurvey.social.length-1;i++) {
if (newCourse.equals(RateSurvey.social[i])) return survey[currentQuestion][i];
}
}
因此,如果程序到达代码的这一部分并且最终没有返回survey[currentQuestion][i]
,那么它将跳到if-else链的末尾并且不返回任何内容。你应该把它们改成这样的东西:
else if (RateDriver.levelText.equals("social studies")) {
for (int i = 0; i < RateSurvey.social.length-1;i++) {
if (newCourse.equals(RateSurvey.social[i])) return survey[currentQuestion][i];
}
return 100;
}
答案 1 :(得分:2)
只需删除最后一个else
之前的return 100;
即表示对于巨大的if
语句无法处理的每个案例都会发生这种情况。
它可能是相同的,但它只是使代码更清晰。
答案 2 :(得分:0)
您需要在收到错误之后添加一个额外的括号。
以下内容未出现任何编译错误:
public static int findNumber(String course, int currentQuestion) {
String newCourse = course.toLowerCase();
if (RateDriver.levelText.equals("math")) {
for (int i = 0; i < RateSurvey.math.length-1;i++) {
if (newCourse.equals(RateSurvey.math[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("science")) {
for (int i = 0; i < RateSurvey.science.length-1;i++) {
if (newCourse.equals(RateSurvey.science[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("english")) {
for (int i = 0; i < RateSurvey.english.length-1;i++) {
if (newCourse.equals(RateSurvey.english[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("social studies")) {
for (int i = 0; i < RateSurvey.social.length-1;i++) {
if (newCourse.equals(RateSurvey.social[i])) return survey[currentQuestion][i];
}
}
else if (RateDriver.levelText.equals("art")) {
for (int i = 0; i < RateSurvey.art.length-1;i++) {
if (newCourse.equals(RateSurvey.art[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("pe")) {
for (int i = 0; i < RateSurvey.pe.length-1;i++) {
if (newCourse.equals(RateSurvey.pe[i])) return survey[currentQuestion][i];
}
}
else if (RateDriver.levelText.equals("chinese")) {
for (int i = 0; i < RateSurvey.chinese.length-1;i++) {
if (newCourse.equals(RateSurvey.chinese[i])) return survey[currentQuestion][i];
}
}
else if (RateDriver.levelText.equals("japanese")) {
for (int i = 0; i < RateSurvey.japanese.length-1;i++) {
if (newCourse.equals(RateSurvey.japanese[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("french")) {
for (int i = 0; i < RateSurvey.french.length-1;i++) {
if (newCourse.equals(RateSurvey.french[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("spanish")) {
for (int i = 0; i < RateSurvey.spanish.length-1;i++) {
if (newCourse.equals(RateSurvey.spanish[i])) return survey[currentQuestion][i];
}
return 100;
}
else if (RateDriver.levelText.equals("hawaiian")) {
for (int i = 0; i < RateSurvey.hawaiian.length-1;i++) {
if (newCourse.equals(RateSurvey.hawaiian[i])) return survey[currentQuestion][i];
}
return 100;
}
else return 100;
}
} //ADDED