简体   繁体   English

运行多个 else if 语句

[英]Run multiple else if statements

When I run this code, only the INVALID (over 100) and High Distinction works.当我运行此代码时,只有 INVALID(超过 100)和 High Distinction 有效。 Any number below 80 also shows High Distinction.任何低于 80 的数字也显示高区分度。 What have I done wrong?我做错了什么?

function calculateGrade() {
    var fvalue = Number(prompt('Please enter final score for unit. Enter a whole    number only'));

    document.write('The final score entered is ' + fvalue + '<br />');

    if (fvalue > 100) {
        document.write('INVALID');
    } else if (80 <= fvalue <= 100) {
        document.write('High Distinction');
    } else if (70 <= fvalue <= 79) {
        document.write('Distinction');
    } else if (60 <= fvalue <= 69) {
        document.write('Credit');
    } else if (50 <= fvalue <= 59) {
        document.write('Pass');
    } else if (0 <= fvalue <= 49) {
        document.write('Fail');
    } else if (fvalue < 0) {
        document.write('INVALID');
    }

}

calculateGrade()

Your comparison syntax is invalid.您的比较语法无效。 You need to check one boundary at a time:您需要一次检查一个边界:

if (80 <= fvalue && fvalue <= 100) {

Same for the others.其他人也一样。

To take it a step further, you only need to check one boundary, because the higher end is excluded by the else :更进一步,您只需要检查一个边界,因为else排除了较高的一端:

if (fvalue > 100) {
    document.write('INVALID');
} else if (80 <= fvalue) {
    document.write('High Distinction');
} else if (70 <= fvalue) {
// ...

This isn't java.这不是爪哇。 But you can surely try this.但是你肯定可以试试这个。

else if ( (fvalue >= 80) && (fvalue<= 100)) { document.write('High Distinction'); else if ( (fvalue >= 80) && (fvalue<= 100)) { document.write('High Distinction');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM