javascript-彩票游戏

来源:互联网 发布:数据库的完整性是指 编辑:程序博客网 时间:2024/06/04 19:36
<html> <head>     <meta http-equiv="content-type" content="text/html; charset=utf-8" />     <title>6-4 彩票游戏</title>     <!-- 样式表 -->     <style type="text/css">        * {font-size: 12px;}//规定所有字体样式     </style>     <!-- 脚本部分 -->     <script type="text/javascript">        len = 7;//彩票号码位数       function calc(){        var strNumber,strMatchNumber,strResult,intResult;        strNumber = $("txt_number").value;//获取用户输入的号码        if(strNumber.length!=len||isNaN(strNumber)){            alert("输入不符合要求");return;        }//判断是否符合要求        $("txt_money").value -= 2;        //生成中奖号码        strMatchNumber = "";        for(var i=0;i<len;++i){            strMatchNumber += parseInt(Math.random()*10);        }        //输出中奖号码        $("txt_match_number").value = strMatchNumber;        //判断是否中奖        switch(intResult = test_match(strMatchNumber,strNumber)){            //中奖的话输出提示,并返回现金给用户            case 2:case 3:case 4:case 5:case 6:case 7:            $("txt_result").value = "恭喜你中了"+["特","一","二","三","四","五"][len-intResult]+"等奖,获得了"+(5000000/Math.pow(10,len-intResult))+"元";            $("txt_money").value = parseInt($("txt_money").value)+5000000/Math.pow(10,len-intResult);break;            //只有一位数字和中奖号码相同            case 1:            $("txt_result").value = "可惜只差一点就中奖了,加油啊";break;            //所有数字全都不同            case 0:            default:            $("txt_result").value = "真可惜没有中奖...";        }        //如果用户的钱已经用完        if($("txt_money").value<1){            if(confirm("你已经用光了所有的钱,还要再来一次吗?")){                //重来                $("txt_money").value = 10;            } else {                //关闭窗口                window.close();            }        }       }       //判断有几位数字相同       function test_match(str1,str2){        var result = new Array(),matched = 0;        //循环判断每一位数字        for(var i=0;i<len;++i){            if(str1.charAt(i)==str2.charAt(i)){                //如果第i个数字相同,则将相符的字符数加一                matched++;            } else if(matched>0){                //如果第i个数字不同,且前面有matched个位数相同,则将相符的字符数保存在数组result中                result.push(matched);                //清除前面字符的相同情况                matched = 0;            }        }        //如果直到循环结束,两者都相同,保存相同的位数        if(matched>0)result.push(matched);        //判断两者最大的相符位数        result.sort();        return (result.pop());       }       function $(str){return (document.getElementById(str));}     </script> </head> <body style="overflow:auto;">   <table>       <tr>           <td>现有资金:</td>           <td><input id="txt_money" value="10" size="7" readonly>元</td>       </tr>       <tr>           <td>输入购买的彩票号码(7位数)</td>           <td><input id="txt_number" size="7" maxlength="7"></td>       </tr>       <tr>           <td><input type="button" value="开奖" onclick="calc();"></td>       </tr>       <tr>           <td>本期开奖号码:</td>           <td><input id="txt_match_number" size="7" readonly></td>       </tr>       <tr>           <td>结果:</td>           <td><input id="txt_result" size="30" readonly></td>       </tr>   </table> </body></html>

0 0
原创粉丝点击