老师看看可以吗 ?

来源:4-9 编程练习

Mr丶Zhang小

2018-02-28 12:31:04

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Math.random()</title>
</head>
<body>
<script>
    
    //获取随机函数
    function getRandom(x,y){
        k = y - x + 1;
        return Math.floor(Math.random()*k+x);
    }
    //猜数字
    function guessNum(num){
        var n = getRandom(1,9);
        document.write(n);
        while(n != num){
            if(n >num){
                alert("你输入的数字小了");
            }else{
                alert("你输入的数字大了");
            }
             var num = parseInt(prompt("请输入你猜的数字")); 
        }
        if(n == num){
                alert("恭喜你输入正确");
        }
    }
    //调用该函数
    var numInput = parseInt(prompt("请输入你猜的数字"));
    guessNum(numInput);
</script>
</body>
</html>


写回答

1回答

怎么都被占用了呢

2018-02-28

可以呢,还有n等于num的时候,就不会走while循环了,所以直接这么写就可以的

http://img.mukewang.com/climg/5a96436a0001491508830444.jpg

0

0 学习 · 36712 问题

查看课程