?? 6-3 彩票游戲.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 + -