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
你的代码有进行测试么?运行的最后结果是进入了死循环,一致弹出"输入的数字小了";
这个思路是不对的,可以参考我上面给出的代码~
另:如果有问题,请重新发布一个新问题去进行提问!
小丸子爱吃菜
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>
祝学习愉快!
相似问题