繁体   English   中英

运行多个 else if 语句

[英]Run multiple else if statements

当我运行此代码时,只有 INVALID(超过 100)和 High Distinction 有效。 任何低于 80 的数字也显示高区分度。 我做错了什么?

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()

您的比较语法无效。 您需要一次检查一个边界:

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

其他人也一样。

更进一步,您只需要检查一个边界,因为else排除了较高的一端:

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

这不是爪哇。 但是你肯定可以试试这个。

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

暂无
暂无

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

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