js中switch语句是可以使用变量,表达式,字符串,布尔值等来作为参数的!
例如:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script type="text/javascript">
var score=parseInt(prompt('请输入你的成绩'));
switch(true){case score>=0&&score<60:alert('不及格');break;case score>60&&score<=70:alert('及格');break;case score>70&&score<=80:alert('良好');breakcase score>80&&score<=90:alert('优秀');break; case score>90&&score<=100:alert('优秀');break; default:alert('输入不合法');
}
</script>
</body>
</html>
结果:
例2:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script type="text/javascript">var grade=parseInt(prompt("请你输入您的成绩!"));var g=parseInt(grade/10);switch(g){case 1:case 2:case 3:case 4:case 5:case 6:alert("您的成绩为及格");break;case 7:case 8:case 9:break;alert("您的成绩为良好");case 10:alert("您的成绩为优秀!");break;default:alert("buzhidao");}
</script>
</body>
</html>
也可以实现!
参数也可以是字符串!
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script type="text/javascript">var i=prompt("请你输入人称");switch(i){case "我":alert("您匹配的是正确的!");break;case "你":alert("您匹配的是不正确的!");break;default:alert("buzhidao");}
</script>
</body>
</html>
结果如下: