[英]JavaScript if/else if/else statement is not working
这是我的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");
}
一切正常,除非我最后到达else语句。 它仅适用于大于3XL衬衫的数字。 但是,如果数字是来自6个类别的测量值的组合,则不会打印else语句。 有人知道我在做什么错吗?
从逻辑角度考虑这一部分:
// Small Shirts
if(shirtWidth >= 18 && shirtWidth < 20) {
if(shirtLength >= 28 && shirtLength < 29)
if(shirtSleeve >= 8.13 && shirtSleeve < 8.38) {
console.log("S");
}
}
else // ...
因此,如果shirtWidth
匹配第一个条件,则分支到if
的正文中。 但是,如果shirtLength
或shirtSleeve
与那里的条件不匹配,则您什么也不做。 完全没有 因为else
没有连接到他们。 它连接到if(shirtWidth >= 18 && shirtWidth < 20)
。
将条件与&&
组合在一起,并仅使用if
的单层:
// Small Shirts
if (shirtWidth >= 18 && shirtWidth < 20 &&
shirtLength >= 28 && shirtLength < 29 &&
shirtSleeve >= 8.13 && shirtSleeve < 8.38) {
console.log("S");
}
// Medium Shirts
else // ...
话虽如此,除了语法之外,我怀疑代码的逻辑不是您想要的。 如果shirtWidth
为19但shirtLength
为30怎么办? 您的案件都无法解决。
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"); }
您将需要在每个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");
}
不是最好的方法,但这应该可以解决您的问题。
这就是我最终要做的事情:
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.