简体   繁体   English

JavaScript if / else if / else语句不起作用

[英]JavaScript if/else if/else statement is not working

This is my if/else if/ else statement: 这是我的if / else if / else语句:

var shirtWidth = 18;
var shirtLength = 33;
var shirtSleeve = 8.63;

// Small Shirts
if(shirtWidth >= 18 && shirtWidth < 20) {
        if(shirtLength >= 28 && shirtLength < 29)
        if(shirtSleeve >= 8.13 && shirtSleeve < 8.38) {
            console.log("S");
        }
}
// Medium Shirts
else if(shirtWidth >= 20 && shirtWidth < 22) {
        if(shirtLength >= 29 && shirtLength < 30)
        if(shirtSleeve >= 8.38 && shirtSleeve < 8.63) {
            console.log("M");
        }
}
// Large Shirts
else if(shirtWidth >= 22 && shirtWidth < 24) {
        if(shirtLength >= 30 && shirtLength < 31)
        if(shirtSleeve >= 8.63 && shirtSleeve < 8.88) {
            console.log("L");
        }
}
// XL Shirts
else if(shirtWidth >= 24 && shirtWidth < 26) {
        if(shirtLength >= 31 && shirtLength < 33)
        if(shirtSleeve >= 8.88 && shirtSleeve < 9.63) {
            console.log("XL");
        }
}
// 2XL Shirts
else if(shirtWidth >= 26 && shirtWidth < 28) {
        if(shirtLength >= 33 && shirtLength < 34)
        if(shirtSleeve >= 9.63 && shirtSleeve < 10.13) {
            console.log("2XL");
        }
}
// 3XL Shirts
else if(shirtWidth === 28) {
        if(shirtLength === 34)
        if(shirtSleeve === 10.13) {
            console.log("3XL");
        }
}
// Does not match any shirt sizes
else {
    console.log("N/A");
}

Everything works except when I get to the else statement at the end. 一切正常,除非我最后到达else语句。 It only works for numbers greater than the 3XL shirts. 它仅适用于大于3XL衬衫的数字。 However, if the numbers are combination of measurements from the 6 categories, the else statement does not print. 但是,如果数字是来自6个类别的测量值的组合,则不会打印else语句。 Does anyone know what I am doing wrong? 有人知道我在做什么错吗?

Consider this part from a logic perspective: 从逻辑角度考虑这一部分:

// Small Shirts
if(shirtWidth >= 18 && shirtWidth < 20) {
        if(shirtLength >= 28 && shirtLength < 29)
        if(shirtSleeve >= 8.13 && shirtSleeve < 8.38) {
            console.log("S");
        }
}
else // ...

So if shirtWidth matches that first condition, you branch into the body of the if . 因此,如果shirtWidth匹配第一个条件,则分支到if的正文中。 But if shirtLength or shirtSleeve don't match the conditions there, you don't do anything. 但是,如果shirtLengthshirtSleeve与那里的条件不匹配,则您什么也不做。 At all. 完全没有 Because the else isn't connected to them. 因为else没有连接到他们。 It's connected to the if(shirtWidth >= 18 && shirtWidth < 20) . 它连接到if(shirtWidth >= 18 && shirtWidth < 20)

Combine your conditions with && and use only a single layer of if s: 将条件与&&组合在一起,并仅使用if的单层:

// Small Shirts
if (shirtWidth >= 18 && shirtWidth < 20 &&
    shirtLength >= 28 && shirtLength < 29 &&
    shirtSleeve >= 8.13 && shirtSleeve < 8.38) {
    console.log("S");
}
// Medium Shirts
else // ...

Having said that, aside from syntax, I suspect the logic of the code isn't quite what you want. 话虽如此,除了语法之外,我怀疑代码的逻辑不是您想要的。 What if a shirtWidth is 19 but shirtLength is 30? 如果shirtWidth为19但shirtLength为30怎么办? None of your cases handles that. 您的案件都无法解决。

TJ Crowder's answer is perfect (and should be accepted), but I might suggest separating your conditions to make it easier to read and manage: TJ Crowder的答案是完美的(应该接受),但我建议您分离条件以使其更易于阅读和管理:

 var shirtWidth = 18; var shirtLength = 33; var shirtSleeve = 8.63; var isSmall = shirtWidth >= 18 && shirtWidth < 20 && shirtLength >= 28 && shirtLength < 29 && shirtSleeve >= 8.13 && shirtSleeve < 8.38; var isMedium = shirtWidth >= 20 && shirtWidth < 22 && shirtLength >= 29 && shirtLength < 30 && shirtSleeve >= 8.38 && shirtSleeve < 8.63; var isLarge = shirtWidth >= 22 && shirtWidth < 24 && shirtLength >= 30 && shirtLength < 31 && shirtSleeve >= 8.63 && shirtSleeve < 8.88; var isXL = shirtWidth >= 22 && shirtWidth < 24 && shirtLength >= 31 && shirtLength < 33 && shirtSleeve >= 8.88 && shirtSleeve < 9.63; var isXXL = shirtWidth >= 26 && shirtWidth < 28 && shirtLength >= 33 && shirtLength < 34 && shirtSleeve >= 9.63 && shirtSleeve < 10.13; var is3XL = shirtWidth === 28 && shirtLength === 34 && shirtSleeve === 10.13; if(isSmall) { console.log("S"); } else if(isMedium) { console.log("M"); } else if(isLarge) { console.log("L"); } else if(isXL) { console.log("XL"); } else if(isXXL) { console.log("2XL"); } else if(is3XL) { console.log("3XL"); } else {// Does not match any shirt sizes console.log("N/A"); } 

You will need to add a return statement at each console.log statement or combine the if conditions using the && operator. 您将需要在每个console.log语句中添加return语句,或使用&&运算符组合if条件。

var shirtWidth = 18;
var shirtLength = 33;
var shirtSleeve = 8.63;

// Small Shirts
if (shirtWidth >= 18 &&
    shirtWidth < 20 &&
    shirtLength >= 28 &&
    shirtLength < 29 &&
    shirtSleeve >= 8.13 &&
    shirtSleeve < 8.38) {
    console.log("S");
}
// Medium Shirts
else if (shirtWidth >= 20 &&
    shirtWidth < 22 &&
    shirtLength >= 29 &&
    shirtLength < 30 &&
    shirtSleeve >= 8.38 &&
    shirtSleeve < 8.63) {
    console.log("M");
}
// Large Shirts
else if (shirtWidth >= 22 &&
    shirtWidth < 24 &&
    shirtLength >= 30 &&
    shirtLength < 31 &&
    shirtSleeve >= 8.63 &&
    shirtSleeve < 8.88) {
    console.log("L");
}
// XL Shirts
else if (shirtWidth >= 24 &&
    shirtWidth < 26 &&
    shirtLength >= 31 &&
    shirtLength < 33 &&
    shirtSleeve >= 8.88 && shirtSleeve < 9.63) {
    console.log("XL");
}
// 2XL Shirts
else if (shirtWidth >= 26 &&
    shirtWidth < 28 &&
    shirtLength >= 33 &&
    shirtLength < 34 &&
    shirtSleeve >= 9.63 &&
    shirtSleeve < 10.13) {
    console.log("2XL");
}
// 3XL Shirts
else if (shirtWidth === 28 &&
    shirtLength === 34 &&
    shirtSleeve === 10.13) {
    console.log("3XL");

}
// Does not match any shirt sizes
else {
    console.log("N/A");
}

Not the best possible way to do this, but this should solve your problem. 不是最好的方法,但这应该可以解决您的问题。

This is what I ended up doing: 这就是我最终要做的事情:

var shirtWidth = 24;
var shirtLength = 28;
var shirtSleeve = 10;

// Small Shirts
if(shirtWidth >= 18 && shirtWidth < 20 &&
    shirtLength >= 28 && shirtLength < 29 &&
    shirtSleeve >= 8.13 && shirtSleeve < 8.38) {
        console.log("S");
    }
// Medium Shirts
else if(shirtWidth >= 20 && shirtWidth < 22 &&
    shirtLength >= 29 && shirtLength < 30 &&
    shirtSleeve >= 8.38 && shirtSleeve < 8.63) {
        console.log("M");
    }
// Large Shirts
else if(shirtWidth >= 22 && shirtWidth < 24 &&
    shirtLength >= 30 && shirtLength < 31 &&
    shirtSleeve >= 8.63 && shirtSleeve < 8.88) {
        console.log("L");
    }
// XL Shirts
else if(shirtWidth >= 24 && shirtWidth < 26 &&
    shirtLength >= 31 && shirtLength < 33 &&
    shirtSleeve >= 8.88 && shirtSleeve < 9.63) {
        console.log("XL");
    }
// 2XL Shirts
else if(shirtWidth >= 26 && shirtWidth < 28 &&
    shirtLength >= 33 && shirtLength < 34 &&
    shirtSleeve >= 9.63 && shirtSleeve < 10.13) {
        console.log("2XL");
    }
// 3XL Shirts
else if(shirtWidth === 28 &&
    shirtLength === 34 &&
    shirtSleeve === 10.13) {
        console.log("3XL");
    }
// Does not match any shirt sizes
else {
    console.log("N/A");
}

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

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