Eric explained nicely how to do what you seem to be trying to accomplish, but let me clear up where you went wrong.
A switch/case structure compares a given variable (the switch argument) to possible values (the case arguments) and then executes the code between the matching case statement and the next break statement (or, if the language does not support fall-through, before the next case statement).
What you're trying to do is not to compare a variable to constant expressions, but to compare a variable against conditions. An if/elseif structure would probably be a cleaner way to express it:
if (this.yourNumberGrade >= 0.90) {
this.yourLetterGrade = 'A';
} else if (this.yourNumberGrade >= 0.80) {
this.yourLetterGrade = 'B';
} else if (this.yourNumberGrade >= 0.70) {
this.yourLetterGrade = 'C';
} else if (this.yourNumberGrade >= 0.60) {
this.yourLetterGrade = 'D';
} else { // you left the default out, but I assume this will be an F for Failed
this.yourLetterGrade = 'F';
}
If you want it shorter, you could try experimenting with the ternary operator like so:
this.yourLetterGrade = (
this.yourNumberGrade >= 0.90 ? 'A' : (
this.yourNumberGrade >= 0.80 ? 'B' : (
this.yourNumberGrade >= 0.70 ? 'C' : (
this.yourNumberGrade >= 0.60 ? 'D' : 'F'
)
)
)
)
As you can see, this costs you a LOT of readability, so if/else is probably the cleanest way to do it.
What Eric was trying to show you is a structure like this:
switch (true) { // We compare the boolean constant "true" to the case arguments
case this.yourNumberGrade >= 0.90:
// this is a boolean expression and evaluates either
// to "true" (matches the switch argument) or
// to "false" (does not match the switch argument)
this.yourLetterGrade = 'A';
break;
case this.yourNumberGrade >= 0.80:
this.yourLetterGrade = 'B';
break;
case this.yourNumberGrade >= 0.70:
this.yourLetterGrade = 'C';
break;
case this.yourNumberGrade >= 0.90:
this.yourLetterGrade = 'D';
break;
default:
// This is executed if none of the case arguments evaluate
// to the value of the switch argument.
this.yourLetterGrade = 'F';
// No break needed, because the end of the switch structure follows:
}
I hope that clears it up for you. You probably have to pay more attention to the exact semantics of the structures you are trying to use. These structures are very similar in most languages.
For kicks and giggles, you could even do it with an array:
// Our letter grades in ascending order (from bad to good).
String letterGrades[] = {'F','D','C','B','A'};
// Our number grade is in the range [0.0;1.0]. As floating point numbers are
// too precise for indexes, we want to round them down to the cut-off
// (0.9, 0.8, etc) and turn them into integer values we can use as array indices.
int gradeIndex = (int) Math.floor(this.yourNumberGrade*10);
// The lowest cut-off is 0.6, so we can treat everything lower than that the same
gradeindex = gradeindex - 5;
gradeIndex = Math.max(gradeIndex, 0);
// With Math.max we have ensured that no index can be lower than 0, now we need
// to make sure that no index is larger than the largest index in our array
// (which by definition is equal to the array's length (i.e. number of elements)
// minus 1 (because the lowest index is 0, an array of e.g. size 4 has the
// indices 0,1,2,3, but lacks an index 4 -- better get used to it, that's how
// programmers count, too).
gradeIndex = Math.min(gradeIndex, letterGrades.length-1);
// Now that our index is clean and guaranteed to be within range, we can use it
// to look up the letter grade:
this.yourLetterGrade = letterGrades[gradeIndex];
Without comments and with a few shorthands, this is even shorter:
// Grades are as follows: A: 90%+, B: 80%+, C: 70%+, D: 60%+, F: <60%
String letterGrades[] = {'F','D','C','B','A'};
int gradeIndex = Math.min(
Math.max(0, (int) Math.floor(this.yourNumberGrade*10) - 5),
letterGrades.length-1
);
this.yourLetterGrade = letterGrades[gradeIndex];
Note that this makes, however, less clear, where the exact cut-off points for the letter grades are, which is why it needs comments. Also, you'll have a problem if the cut-offs change for any reason (e.g. A: 85%+ or F: <66.6%). You could still adjust the calculation (the Math.floor(this.yourNumberGrade*10)-5
part), but this will make it even harder to follow and won't help if the grades aren't merely gradual. For traditional systems, however, it's a quick and easy way to do it.