練習 Android 的時候,用到猜數字的範例。為了猜快一點,所以用 Javascript 寫了一個輔助程式。
<script type="text/javascript"> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=BIG5"> <title>Insert title here</title> <!-- 參考 http://blog.xuite.net/funnyfamily/tonyjan/21045725-%EF%BD%9EC%E8%AA%9E%E8%A8%80%E7%A8%8B%E5%BC%8F%E8%A8%AD%E8%A8%88%EF%BD%9E%E9%9B%BB%E8%85%A6%E7%8C%9C%E6%95%B8%E5%AD%97%EF%BD%9E --> <script type="text/javascript"> var arrAnswers = new Array(); for ( var i = 1234; i < 9876; i++) { n1 = i.toString().substring(0, 1); //千位數 n2 = i.toString().substring(1, 2); //百位數 n3 = i.toString().substring(2, 3); //十位數 n4 = i.toString().substring(3, 4); //個數位 if (n1 == n2 || n1 == n3 || n1 == n4 || n2 == n3 || n2 == n4 || n3 == n4) { //去除重複 continue; } if(n1==0 || n2==0 || n3==0 || n4 ==0){ //去除0 continue; } //arrAnswers第一維的key值是4位數的數字,第二維則是把4位數再拆成4個個別的數字 arrAnswers[i] = new Array(4); arrAnswers[i][0] = n1; //千位數 arrAnswers[i][1] = n2; //百位數 arrAnswers[i][2] = n3; //十位數 arrAnswers[i][3] = n4; //個數位 } function guess(obj){ var id = obj.getAttribute("id"); var r = id.substr(4,1); //猜了幾次 for(n = 1; n <= r; n++){ var objInputN = document.getElementById("inputN"+n); var objInputA = document.getElementById("inputA"+n); var objInputB = document.getElementById("inputB"+n); //檢查是否輸入數字 if(objInputN.value.length==0 || objInputA.value.length==0){ alert("請依序輸入每一組的數字、幾A幾B"); return; } var arrInput = new Array(4); arrInput[0] = objInputN.value.substring(0, 1); //千位數 arrInput[1] = objInputN.value.substring(1, 2); //百位數 arrInput[2] = objInputN.value.substring(2, 3); //十位數 arrInput[3] = objInputN.value.substring(3, 4); //個數位 for(key in arrAnswers){ //檢查所有可能數字組合 var countA = countB = 0; for(i = 0;i < 4;i++){ //當前檢查的數字 for(j = 0;j < 4;j++){ //輸入的數字 if(arrAnswers[key][i]==arrInput[j]){ if(i == j) countA++; else countB++; } } } if( !(countA == objInputA.value && countB == objInputB.value)){ //移除不符合的數字 delete arrAnswers[key]; } } } var str = ""; var c = 0; for(key in arrAnswers){ if(arrAnswers[key]=='undefined'){ continue; } if(c < 51){ str += key + "<BR>"; } c++; } document.getElementById("divResult").innerHTML = "目前共有 " + c + "組 <BR>" + str; } </script> </head> <body> <table border="1" cellpadding="3"> <tr> <td width="400"> <form name="form1" method="post" action=""> <table width="100%" border="0" cellpadding="3"> <tr> <td> 數字: <label><input name="inputN1" type="text" id="inputN1" value="3724"></label> , <label><input name="inputA1" type="text" id="inputA1" value="0" size="3"> A </label> <label><input name="inputB1" type="text" id="inputB1" value="0" size="3"> B </label> <label><input type="button" name="send1" id="send1" value="送出" onClick="guess(this)"></label> </td> </tr> <tr> <td> 數字: <label><input name="inputN2" type="text" id="inputN2" value="8196"></label> , <label><input name="inputA2" type="text" id="inputA2" value="0" size="3"> A </label> <label><input name="inputB2" type="text" id="inputB2" value="0" size="3"> B </label> <label><input type="button" name="send2" id="send2" value="送出" onClick="guess(this)"></label> </td> </tr> <tr> <td> 數字: <label><input name="inputN3" type="text" id="inputN3"></label> , <label><input name="inputA3" type="text" id="inputA3" value="0" size="3"> A </label> <label><input name="inputB3" type="text" id="inputB3" value="0" size="3"> B </label> <label><input type="button" name="send3" id="send3" value="送出" onClick="guess(this)"></label> </td> </tr> <tr> <td> 數字: <label><input name="inputN4" type="text" id="inputN4"></label> , <label><input name="inputA4" type="text" id="inputA4" value="0" size="3"> A </label> <label><input name="inputB4" type="text" id="inputB4" value="0" size="3"> B </label> <label><input type="button" name="send4" id="send4" value="送出" onClick="guess(this)"></label> </td> </tr> <tr> <td> 數字: <label><input type="text" name="inputN5" id="inputN5"></label> , <label> <input name="inputA5" type="text" id="inputA5" value="0" size="3"> A </label> <label><input name="inputB5" type="text" id="inputB5" value="0" size="3"> B </label> <label><input type="button" name="send5" id="send5" value="送出" onClick="guess(this)"></label> </td> </tr> <tr> <td> 數字: <label><input type="text" name="inputN6" id="inputN6"></label> , <label><input name="inputA6" type="text" id="inputA6" value="0" size="3"> A </label> <label><input name="inputB6" type="text" id="inputB6" value="0" size="3"> B </label> <label><input type="button" name="send6" id="send6" value="送出" onClick="guess(this)"></label> </td> </tr> <tr> <td> 數字: <label><input type="text" name="inputN7" id="inputN7"></label> , <label><input name="inputA7" type="text" id="inputA7" value="0" size="3"> A </label> <label><input name="inputB7" type="text" id="inputB7" value="0" size="3"> B </label> <label><input type="button" name="send7" id="send7" value="送出" onClick="guess(this)"></label> </td> </tr> <tr> <td> 數字: <label><input type="text" name="inputN8" id="inputN8"></label> , <label><input name="inputA8" type="text" id="inputA8" value="0" size="3"> A </label> <label><input name="inputB8" type="text" id="inputB8" value="0" size="3"> B </label> <label><input type="button" name="send8" id="send8" value="送出" onClick="guess(this)"></label> </td> </tr> <tr> <td> 數字: <label><input type="text" name="inputN9" id="inputN9"></label> , <label><input name="inputA9" type="text" id="inputA9" value="0" size="3"> A </label> <label><input name="inputB9" type="text" id="inputB9" value="0" size="3"> B </label> <label><input type="button" name="send9" id="send9" value="送出" onClick="guess(this)"></label> </td> </tr> <tr> <td> 數字: <label><input type="text" name="inputN10" id="inputN10"></label> , <label><input name="inputA10" type="text" id="inputA10" value="0" size="3"> A </label> <label><input name="inputB10" type="text" id="inputB10" value="0" size="3"> B </label> <label><input type="button" name="send10" id="send10" value="送出" onClick="guess(this)"></label> </td> </tr> </table> </form> </td> <td width="200">最多顯示50組<BR><BR><div id="divResult" style="height:400px; overflow:auto"></div></td> </tr> </table> </body> </html>
.
沒有留言:
張貼留言