繁体   English   中英

多个if-else无法正常工作

[英]Multiple if-else isn't working

var x = prompt("number between 50 and 100");

if (x.match(/hi/)) {
  alert("cool");
} else {
  alert("neni cool");
} else if (x==50 || x==100) {
  alert("skevele");
} else {
  alert("nic");
}

谁能解释为什么如果不起作用呢? 感谢您的回答。

您不能像这样堆叠它们。 }else{语句仅是if / then / else语句中的最后一种情况,因为只有在其他情况没有时才这样做。

if(condition){
  what to do
}else if(condition 2){
  what to do if the first condition was not met
}else{
  if all else fails
}

else ifelse之后,你就在else if else else if将无法正确评估,因为else结束了if语句。 如果您希望这两个语句都起作用,请尝试:

f (x.match(/hi/)) {
  alert("cool");
} else {
  alert("neni cool");
}

if (x==50 || x==100) {
  alert("skevele");
} else {
  alert("nic");
}

暂无
暂无

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

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