我有以下JavaScript代码:
var myAge = parseInt ( prompt("Enter your age", 30), 10 );
if (myAge >= 0 && myAge <= 10) {
document.write("myAge is between 0 and 10 <br>");
}
if (!(myAge >= 0 && myAge <= 10)) {
document.write("myAge is NOT between 0 and 10<br>");
}
if (myAge >= 80 || myAge <= 10) {
document.write("myAge is 80 or above OR 10 or below<br>");
}
if ( (myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89) ) {
document.write("myAge is between 30 and 39 or myAge is " + "between 80 and 89");
}
我的任务是将其转换为switch语句。我试图这样做,但我想出了这段代码,但是当我在提示中输入数字时,文本不会显示:
var myAge = parseInt ( prompt("Enter your age", 30), 10 );
switch(myAge) {
case myAge >= 0 && myAge <= 10 :
document.write("myAge is between 0 and 10 <br>");
break;
case !(myAge >= 0 && myAge <= 10) :
document.write("myAge is NOT between 0 and 10<br>");
break;
case myAge >= 80 || myAge <= 10 :
document.write("myAge is 80 or above OR 10 or below<br>");
break;
case (myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89) :
document.write("myAge is between 30 and 39 or myAge is " + "between 80 and
89");
break;
}
这与我的休息时间有关吗?事情是?
答案 0 :(得分:0)
开关并不是真的可以这样工作。
如果您想使用开关,可以使用switch(true)代替:
var myAge = parseInt ( prompt("Enter your age", 30), 10 );
switch(true) {
*
*
*
}