4-7的编程,麻烦老师帮忙看看

来源:5-1 date(1)

魏妮宝贝

2017-11-09 11:12:43

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Math.random()</title>

</head>

<body>

<script>

var Num=Math.floor(Math.random()*9+1);

document.write(Num);

var input=Math.floor(prompt("请输入你猜的数字"));

while (Num!=input){

input=Math.floor(prompt("请输入你猜的数字"));

if (isNaN(input)) {

alert("请输入正确的数字,不能有其他字符");

    }else if (input>Num) {

alert("输入的数字大了");

    }else if(input<Num){

alert("输入的数字小了");

}else{

alert("恭喜你猜对了");

    }

}

</script>

</body>

</html>

如果输入为空的话,就是按照小了 来提示,这样可以不

写回答

2回答

小丸子爱吃菜

2017-11-09

你的代码有进行测试么?运行的最后结果是进入了死循环,一致弹出"输入的数字小了";

这个思路是不对的,可以参考我上面给出的代码~

另:如果有问题,请重新发布一个新问题去进行提问!

0

小丸子爱吃菜

2017-11-09

会弹出两次输入框,将前面的去掉,直接声明就可以。

也可以判断当输入为空时,提醒它输入不能为空。

 <script>

    var Num = Math.floor(Math.random() * 9 + 1);

    document.write(Num);

    var input;

    while (Num != input) {

        input = Math.floor(prompt("请输入你猜的数字"));

        if (isNaN(input)) {

            alert("请输入正确的数字,不能有其他字符");

        } else if (input > Num) {

            alert("输入的数字大了");

        } else if (input < Num) {

            alert("输入的数字小了");

        } else {

            alert("恭喜你猜对了");

        }

    }

    </script>

祝学习愉快!

0
hq_青蛙妹_0
h while 里面的循环条件就是输入不等于随机数;这样里面的if条件里面就不可能会有猜对的情况,这样的思路对吗? 一下代码请帮忙看看,有什么问题,感谢! var num=Math.floor(Math.random()*9+1); var a=prompt("请输入数字"); input=Number(a); console.log(input); console.log(num); if(input==num){alert("恭喜您猜对了!");}; while(input!=num){ if(input>num){alert("输入的数字大了")} else{alert("输入的数字小了")} }
h017-11-09
共1条回复

0 学习 · 36712 问题

查看课程