XIN'BOLG

导航

4月6日--关于随机数的练习题--猜数字

猜数

1.随机产生一个(0,100)之间的随机数

2.使用prompt让用户猜随机数,如果猜错,提示随机数大于还 是小于用户猜的数


3.然后继续让用户猜,直到用户猜对为止,输出用户猜数的次数

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript">


        var n=Math.floor(Math.random()*99)+1;
        console.log(n);
        var count=0;
        while(true){
            count+=1;
            var i=prompt("请输入一个数");
            if(i<n){
                alert("小了,请重新输入");
            }else if(i>n){
                alert("大了,请重新输入");
            }
            else{
                alert("终于猜对了,"+"您输入了"+count+"次");
                break
            }
        }



    </script>
</head>
<body>

</body>
</html>

 

posted on 2017-04-08 20:50  XIN'BOLG  阅读(151)  评论(0编辑  收藏  举报