<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>if/else语句的基本使用</title>
</head>
<body>
# 将js代码放入body里面的最底部,这样方便先加载静态显示的内容,再加载js动态效果
<script type="text/javascript">
var age = parseInt(prompt("请您输入一个年龄!"))
console.log(age)
if (age > 18){
console.log("已经成年了,可以喝酒了!")
}else{
console.log("未成年人,请不要喝酒抽烟!!")
}
</script>
</body>
</html>
解释:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>if /else if /else</title>
</head>
<body>
<script>
/*
score 90--100 A级
80--90 B级
70--80 C级
60--70 D级
0-- 60 E级
*/
var score = Number(prompt('请输入您的成绩'))
if (!isNaN(score)){
if (score >= 90 && score<=100){
console.log("A级")
}else if (score > 80){
console.log("B级")
}else if (score > 70){
console.log("C级")
}else if (score > 60){
console.log("D级")
}else{
console.log("E级")
}
}else{
console.log("您输入的数据有误,请输入数字")
}
</script>
</body>
</html>
解释:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
var age = parseInt(prompt("请输入一个年龄"));
result = age >= 18 ? "成年了":"未成年";
console.log(result);
</script>
</body>
</html>
解释:
/*
score 90--100 A级
80--90 B级
70--80 C级
60--70 D级
0-- 60 E级
*/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
var score_level = prompt("请输入成绩等级");
switch (score_level) {
case "A":
console.log("分数大约在90--100之间");
break;
case "B":
console.log("分数大约在80--90之间");
break;
case "C":
console.log("分数大约在70--80之间");
break;
case "D":
console.log("分数大约在60--70之间");
break;
case "E":
console.log("分数大约在0--60之间");
break;
}
</script>
</body>
</html>
解释:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
var montch = parseInt(prompt("请输入月份"));
switch(montch){
case 1:
case 3:
case 5:
case 7:console.log("七月31天");break;
case 6:
case 8:console.log("八月30天");break;
case 2:console.log("二月28天");break;
default:
console.log("输入的数据不符合条件");break;
}
</script>
</body>
</html>
解释:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
var i = 0;
while (i<20){
console.log("hello world", i+1);
i++;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
var username = prompt("请输入用户名!");
var pw = prompt("请输入密码");
while (username != "admin" || pw != "123456") {
username = prompt("请输入用户名!")
pw = prompt("请输入密码")
}
console.log("登录成功")
</script>
</body>
</html>
解释:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
do{
var age = parseInt(prompt("请您输入年龄"));
}while(age<18);
console.log("恭喜您,您刚刚成年了!")
</script>
</body>
</html>
解释:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
var sum = 0;
for (var i = 1; i <= 100; i++) {
if (i%2==0){
sum += i
}
}
console.log("求和为:", sum)
</script>
</body>
</html>
解释:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
// 控制具体打印多少行
for(var l=0;l<5;l++){
// 控制每一行的具体打印情况和换行
for (var i = 1; i <= 5; i++) {
document.write("⭐️")
}
document.write("
")
}
</script>
</body>
</html>
解释:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
// 控制具体打印多少行
for(var l=1;l<=5;l++){
// 控制每一行的具体打印情况和换行
for (var i = 1; i <= l; i++) {
document.write("⭐️")
}
document.write("
")
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
// 控制具体打印多少行---乘法口诀
for(var l=1;l<=9;l++){
// 控制每一行的具体打印情况和换行
for (var i = 1; i <= l; i++) {
document.write(i+"*"+l+"="+i*l,'\t')
}
document.write("
")
}
</script>
</body>
</html>