<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>guess</title>
<script type="text/javascript">
game();
function game() {
num1 = randomNumber();
guess();
}
function randomNumber() {
return Math.round(Math.random()*100);
}
function guess() {
num2 = prompt("请输入一个0~100的数字");
if (isNaN(+num2)) {
alert("请输入有效数字!");
guess();
} else if (num1 > num2) {
alert("小了!");
guess();
} else if (num1 < num2) {
alert("大了!");
guess();
} else {
if (confirm("恭喜答对,是否继续?")) {
game();
} else {
alert("游戏结束");
}
}
}
</script>
</head>
</html>
JS猜数字游戏改良版
最新推荐文章于 2024-01-07 09:04:10 发布