⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 6-3 彩票游戏.htm

📁 JAVASCRIPT完全自学手册,中源码的验证修订实例
💻 HTM
字号:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=GB2312" />
<title>6-3  彩票游戏</title>
<!-- 样式表 -->
<style>
* { font-size:12px; } /*规定了所有的字体样式*/
</style>
<!-- 脚本部分 -->
<script>
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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -