javascript实现的猜数小游戏完整实例代码
dingyan954 人气:0本文实例讲述了javascript实现的猜数小游戏。分享给大家供大家参考,具体如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>js游戏</title> <script type="text/JavaScript"> var x=10; //x=parseInt(Math.random()*(1000-1))+1; for(var i=0;i<10;i++){ var num=prompt("请输入你猜测的数字",""); if (num!=null && num!="") { if(num==x){ switch(i){ case 0: alert('真神啦,猜对了!'); i=10; break; case 1: alert('厉害,对啦!'); i=10; break; case 2: alert('真棒,对啦!'); i=10; break; case 3: alert('好好,对啦!'); i=10; break; case 4: alert('不容易,对啦!'); i=10; break; case 5: alert('将就,对啦!'); i=10; break; case 6: alert('有点慢,对啦!'); i=10; break; case 7: alert('下次快点,对啦!'); i=10; break; case 8: alert('赶上啦,对啦!'); i=10; break; case 9: alert('最后机会,对啦!'); i=10; break; } } if(i==9){ alert('笨蛋,10次都猜不对!'); } if(num>x){ document.write("您已经猜了" + eval(i+1) + "次," + num +"," + "大了!<br>"); } if(num<x){ document.write("您已经猜了" + eval(i+1) + "次," + num +"," + "小了!<br>"); } }else{ alert('不能为空'); } } </script> </head> <body> <div id="div1"></div> </body> </html>
希望本文所述对大家JavaScript程序设计有所帮助。
加载全部内容