<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>猜數字</title>
<!--計算機隨機出一個[0,100]的數字讓用戶去猜,猜對就BINGO,猜大猜小再提示出來,-->
</head>
<body>
<script>
//用隨機函數得到一個隨機數,JS中的random是一個大於等於0,小於1的浮點數。
var computerNum=parseInt(Math.random()*101);
//while外循環控制是否繼續玩遊戲
while (true) {
//while內循環控制是否猜對,可以一直猜。
while (true) {
var num = +prompt("請你輸入你所猜的數字:範圍:[0,100]");
if (computerNum == num) {
alert("BINGO!");
break;
} else if (computerNum > num) {
alert("猜小了!");
} else {
alert("猜大了!");
}
}
var temp=confirm("是否繼續遊戲?");
if(!temp){
alert("Welcome to here again! (*^__^*)goodbye!");
break;
}
}
</script>
</body>
</html>
一個簡單的猜數字遊戲(練習循環結構中的“while”)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.