JS编程猜数字:系统随机生成一个100-500的正整数,用户输入一个数字,如果输入数字大于随机数则提示“大了”,反之“小了”。直到游戏结束,提示“通过”并输出猜出猜测的次数。

var num=parseInt(Math.random()*(500-100)+100);
var input=window.prompt("猜数字:");
var count=0

while(input!=num)
{
if(input>num)
{
count=count+1;
window.alert("大了!");
}
else if(input<num)
{ count=count+1;
window.alert("小了");
}
input=window.prompt("猜数字:");
}
if(input==num)
{
count=count+1;
window.alert("猜对了!"+"猜对次数:"+count);
}

posted @ 2020-08-26 16:08  nan4457  阅读(2149)  评论(0)    收藏  举报