This is a question on my study guide. I am not understanding switches very well.
The questions says a professor determines his grades based on the following table.
score
90-100
80-89
70-79
60-69
<60
grade
A
B
C
D
F
Assign the appropriate value to the variable grade(character) based on the variable score(integer). Use the switch structure.
We were told you are not supposed to use relational operators in JAVA in a switch (char or int).
My thoughts would have been
{
case 1: Grade = "A";
break;
case 2: Grade = "B";
break;
case 3: Grade = "C";
break;
case 4: Grade = "D";
break;
default:
Grade = "F";
}
I just don't know how to work in the grade scale. Any help appreciated.
Comments
[code]
// assuming grade is 0..100, the following should work.
switch (grade/10)
{
case 10: // in case 100 percent
case 9:
Grade = "A";
break;
case 8:
Grade = "B";
break;
case 7:
Grade = "C";
break;
case 6:
Grade = "D";
break;
default:
Grade = "F";
break;
}
[/code]