
需要素材点击图片联系我或私信、评论
需求:用户输入月份,输入对应的天数。
话不多说,直接上代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>根据月份显示对应的天数</title>
</head>
<body>
<script>
var month = Number(prompt("请输入月份:"));
console.log(month);
switch(month){
case 1:
console.log(month+"月有31天");
break;
case 2:
console.log(month+"月有28天");
break;
case 3:
console.log(month+"月有31天");
break;
case 4:
console.log(month+"月有30天");
break;
case 5:
console.log(month+"月有31天");
break;
case 6:
console.log(month+"月有30天");
break;
case 7:
console.log(month+"月有31天");
break;
case 8:
console.log(month+"月有31天");
break;
case 9:
console.log(month+"月有30天");
break;
case 10:
console.log(month+"月有31天");
break;
case 11:
console.log(month+"月有30天");
break;
case 12:
console.log(month+"月有31天");
break;
default:
console.log("月份输入错误!请刷新重新输入!");
}
</script>
</body>
</html>
简写:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>02.根据月份显示对应的天数</title>
</head>
<body>
<script>
var month = Number(prompt("请输入月份"));
console.log(month);
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
console.log(month+"月有31天");
break;
case 2:
console.log(month+"月有28天");
break;
case 4:
case 6:
case 9:
case 11:
console.log(month+"月有30天");
break;
default:
console.log("月份输入错误!请刷新重新输入!");
}
</script>
</body>
</html>