Reputation: 27
all I've been trying to convert the following if statement into a switch statement.
/**
* 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.
}
This was my attempt:
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;
}
When I try to run this, eclipse gives me errors for each case condition that "case expressions must be constant expressions". I am not sure how to overcome this problem. Can anyone help me out? Thanks.
Upvotes: 0
Views: 83
Reputation: 1
Try this instead:
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;
The cause for your exception is that you are trying to do a calculation in the case
statement - which is not allowed (as the exceptions states, only static values are allowed here). The solution above pulls the calculation into the switch
statement and hence fixes your problem.
Upvotes: 0
Reputation: 61168
Items in a case
in a switch
statement must be compile time constant:
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";
}
}
Upvotes: 3
Reputation: 124704
As Eclipse is trying to tell you, this is not possible in Java. You can only use constant expressions in case statements, and that doesn't fit well in your situation.
Anyway, neither if-else nor switch are great here. It would be better to use a table-based solution, for example:
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];
}
Upvotes: 4