Topic: Switch statement Programming language: Java Convert the following functio
ID: 665246 • Letter: T
Question
Topic: Switch statement
Programming language: Java
Convert the following function consisting of a cascaded if statement to a function consisting of a switch statement:
private static char grade(int score)
{
if (score > 89 && <= 109)
return 'A';
else if(score > 79 && score <= 89)
return 'B';
else if(score > 69 && score <= 79)
return 'C';
else if(score > 59 && score <= 69)
return 'D';
else if (score >= 0 && score <= 59)
return 'F';
else
System.out.println("Not a grade");
}
The problem:
private static char grade(int score)
{
Explanation / Answer
java doesn't support conditional switch statements. This is the best way to code this problem.
with switch it is possible but is very irritating
private static char grade(int score)
{
switch (score)
{
case 93:
case 92:
case 91:
case 90:
case 89:
return 'A';
break;
case 88:
case 87:
case 86:
case 85:
case 84:
case 83:
case 82:
case 81:
case 80:
return 'B';
break;
case 75:
case 74:
case 73:
return 'C';
break;
case 62:
case 61:
case 60:
return 'D';
break;
case 1:
case 58:
return 'F';
break;
default:
System.out.println("Not a grade");
return 'G';
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.