switch01
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
var num = prompt("输入成绩");
// switch(+num >= 60){
// case true:
// alert("合格");
// break;
// case false:
// alert("不合格");
// break;
// }
switch(true){
case +num >= 60:
alert("合格");
break;
case +num < 60:
alert("不合格");
break;
}
</script>
</head>
<body>
</body>
</html>
switch02
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript">
var num = prompt("从键盘接收整数参数,如果该数为1-7,打印对应的星期,否则打印非法参数")
switch(+num){
case 1:
alert("星期一");
break;
case 2:
alert("星期二");
break;
case 3:
alert("星期三");
break;
case 4:
alert("星期四");
break;
case 5:
alert("星期五");
break;
case 6:
alert("星期六");
break;
case 7:
alert("星期日");
break;
default:
alert("非法参数");
break;
}
</script>
</head>
<body>
</body>
</html>
总结:switch的用法还可以这么用