if-else statement

前端 未结 3 631
-上瘾入骨i
-上瘾入骨i 2021-01-21 08:57

My codes allows the user to enter in a score from 1 to 100, which will either tell them that the score is \"Good\", \"OK\", \"Moron\", or \"Invalid\".

But, when I compil

3条回答
  •  离开以前
    2021-01-21 08:59

    Each if statement is a standalone conditional statement. Your example has three groups of conditional statements:

    1. if (score >=80 && score <= 100)
    2. if (score >=55 && score <=79)
    3. if (score >=1 && score <=54) { ... } else { ... }

    So if score has the value 55, it will match against #2 above and the else of #3.

    One solution here would be to combine the above statements into one group. You can do this with else if.

    e.g.

    if (*expr*) {
        ...
    } else if (*expr*) {
        ...
    } else if (*expr*) {
        ...
    } else {
        ...
    }
    

提交回复
热议问题