使用JS实现猜数字游戏

使用JS实现猜数字游戏
使用到Math.floor(),将数字转化为小于或等于该数的整数
Math.random(),产生一个随机数0-1之间的数字,包含0不包含1
代码实现

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>猜数游戏</title>
    </head>
    <body>
 
        <script type="text/javascript">
            var num = Math.floor(Math.random() * 100 + 1); //产生1~100之间的随机整数
            do {
                var guess = parseInt(prompt("下面进行猜数游戏\n请输入1-100之间的整数:", ""));
                if (guess == num) {
                    alert("^_^ ,恭喜你,猜对了,幸运数字是:" + num);
                    break;
                } else {
                    if (guess > num) {
                        alert("^_^ ,你猜的数字大了");
                        go_on = confirm("是否继续游戏?");
                    } else {
                        alert("^_^ ,你猜的数字小了");
                        go_on = confirm("是否继续游戏?");
                    }
                }
            } while (go_on);
            alert("谢谢参与游戏!");
        </script>
    </body>
</html>
发布了9 篇原创文章 · 获赞 16 · 访问量 351

猜你喜欢

转载自blog.csdn.net/weixin_43894771/article/details/104747018
今日推荐