老师看看可以吗 ?
来源: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循环了,所以直接这么写就可以的

相似问题