开发者

switch statement doesn't work

how come this java switch stateme开发者_开发知识库nt keeps telling me my statements are not statements

public void setConstant(float inNumGrade)
{
    this.yourNumberGrade = inNumGrade;

    switch (this.yourLetterGrade)
    {
        case 'A':
            this.yourNumberGrade >= 0.90;
            break;

        case 'B':
            this.yourNumberGrade >= .8;
            break;

        case 'C':
            this.yourNumberGrade >= .7;
            break;

         case 'D':
            this.yourNumberGrade >= .6;// not a statement

         default:

    } // end switch
}


I see what you are trying to do, but I think you are going around it the wrong way. What it seems like you are trying to do, is set the "letter grade" based on the switch, not the number grade! I think what you are really trying to do is this:

public void setGrades(float inNumGrade)
{
    this.yourNumberGrade = inNumGrade;

    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
        this.yourLetterGrade= 'F';    
}

You cannot switch on ranges in Java. If you want to do this with a switch, you'd have to do a switch(true), and then do case this.yourNumberGrade>=0.90:

As I expected, you misunderstand how a switch works. If you REALLY need to do this via switch(if/else/else if is better), you'd have to do it this way:

public void setGrades(float inNumGrade)
{
    this.yourNumberGrade = inNumGrade;
    switch(true)
    {
        case this.yourNumberGrade >= 0.90:
            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.60:
            this.yourLetterGrade= 'D';
            break;
        default:
            this.yourLetterGrade= 'F';    
            break;
    }//end switch
}


Because this.yourNumberGrade >= .6; isn't a valid statement like the compiler is telling you. This would be a valid statement:

b = this.yourNumberGrade >= .6;

-- or --

this.yourNumberGrade = .6;

It depends on what you are trying to accomplish.


What exactly are you trying to do? >= is comparison NOT assignment that is why you get the error...just remove > in all places.


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.


You are only doing a comparison which isn't a valid statement in this context.

You probably mean to do an assignment


replace ">=" with "=" if that's what you want to accomplish.


The problem is that you are making a comparison and not assigning the the value. Perhaps you could do something like this:

public void setConstant(float inNumGrade)
{
    this.yourNumberGrade = inNumGrade;

    switch (this.yourLetterGrade)
   {
        case 'A':
            this.yourNumberGrade = 0.90;
            break;

        case 'B':
            this.yourNumberGrade = .8;
            break;

        case 'C':
            this.yourNumberGrade = .7;
            break;

        case 'D':
            this.yourNumberGrade = .6;
        default:

    } // end switch
}

This would actually assign the value to "yourNumberGrade. However this would only be the lower limit of the grade. It might be better to make it where you substitute the "yourNumberGrade" for "yourLetterGrade" and have it determine the letter grade based on the number grade...


You must add a break; inside of each case block.

switch(this.grade){
     case 'A':
         System.out.println("You got an A");
         break;
    default:
         System.out.println("INVALID GRADE");
         break;}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜