所有我一直在尝试将以下if语句转换为switch语句。
/**
* Return appropriate comment for given score and par for hole.
* You can assume that score is not less than 4 below par for hole.
* For example, if score is 4 below par, return "condor!!!!",
* if score is 3 below par, return "albatross!!!",
* if score is 2 below par, return "eagle!!", etc.
* See Assign 2 description for full list of comments.
* @param score
* @param parForHole
* @return appropriate comment for given score and par for hole.
*/
public static String comment(int score, int parForHole) {
if ( score == parForHole-4)
return "condor!!!!";
if ( score == parForHole-3)
return "albatross!!!";
if ( score == parForHole-2)
return "eagle!!";
if ( score == parForHole-1)
return "birdie!";
if ( score == parForHole)
return "par";
if ( score == parForHole+1)
return "bogey";
if ( score == parForHole+2)
return "double bogey";
if ( score == parForHole+3)
return "triple bogey";
return "Not valid"; // Replace by a suitable switch stmt.
}
这是我的尝试:
public static String comment(int score, int parForHole) {
String monthString;
switch (score) {
case parForHole-4: monthString = "condor!!!!";
break;
case parForHole-3: monthString = "albatross!!!";
break;
case parForHole-2: monthString = "eagle!!";
break;
case parForHole-1: monthString = "birdie!";
break;
case parForHole: monthString = "par";
break;
case parForHole+1: monthString = "bogey";
break;
case parForHole+2: monthString = "double bogey";
break;
case parForHole+3: monthString = "triple bogey";
break;
default: monthString = "Invalid";
break;
}
return monthString;
}
当我尝试运行它时,eclipse为每个案例条件提供了错误,即“case表达式必须是常量表达式”。我不知道如何克服这个问题。谁能帮我吗?感谢。
答案 0 :(得分:4)
正如Eclipse试图告诉你的那样,这在Java中是不可能的。 你只能在case语句中使用常量表达式,这在你的情况下并不适合。
无论如何,if-else和switch都不是很好。 最好使用基于表格的解决方案,例如:
static String[] comments = {
"condor!!!!",
"albatross!!!",
"eagle!!",
"birdie!",
"par",
"bogey",
"double bogey",
"triple bogey"
};
public static String comment(int score, int parForHole) {
int index = score - parForHole + 4;
if (index < 0 || index >= comments.length) {
return "Not valid";
}
return comments[index];
}
答案 1 :(得分:3)
case
语句中switch
中的项必须编译时常量:
public static String comment(int score, int parForHole) {
switch (score - parForHole) {
case -4:
return "condor!!!!";
case -3:
return "albatross!!!";
case -2:
return "eagle!!";
case -1:
return "birdie!";
case 0:
return "par";
case 1:
return "bogey";
case 2:
return "double bogey";
case 3:
return "triple bogey";
default:
return "Invalid";
}
}
答案 2 :(得分:0)
请改为尝试:
public static String comment(int score, int parForHole) {
String monthString;
switch (score - parForHole) {
case -4: monthString = "condor!!!!";
break;
case -3: monthString = "albatross!!!";
break;
case -2: monthString = "eagle!!";
break;
case -1: monthString = "birdie!";
break;
case 0: monthString = "par";
break;
case +1: monthString = "bogey";
break;
case +2: monthString = "double bogey";
break;
case +3: monthString = "triple bogey";
break;
default: monthString = "Invalid";
break;
}
return monthString;
您的异常的原因是您正在尝试在case
语句中进行计算 - 这是不允许的(因为异常状态,此处仅允许静态值)。上面的解决方案将计算结果引入switch
语句,从而解决了您的问题。