樂透網站

模擬樂透開獎是一個很經典的題目,在範圍內隨機挑選幾個數字。 用Java要隨機抽取數字不難,只要用java.util.Random()或是Math.random()都可以只用1行程式碼拿到亂數。 經典的部份是如何做到這幾個數字不重複,與眾不同的作法能稱之為藝術。

java.util.Random類別使用系統時間作為種子,
java.lang.Math.Random()方法則是以偽隨機的方式挑選亂數。
理論上都能做到統計學的隨機實驗的散佈標準。


模擬大樂透

以下程式碼模擬大樂透開獎,會產生6個1~49之間不同複的亂數。為了逼真有提供下注的欄位,會顯示下注號碼但是沒有對將程式所以不對獎。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="ch06.jsp" method="POST">
<input type="text" name="ch1" size="2"/> 
<input type="text" name="ch2" size="2"/> 
<input type="text" name="ch3" size="2"/> 
<input type="text" name="ch4" size="2"/> 
<input type="text" name="ch5" size="2"/> 
<input type="text" name="ch6" size="2"/>
<button type="stbmit">確定</button>
</form>
<%if(request.getParameter("ch1")!=null){%>
<hr />
投注號碼:
 
<%=request.getParameter("ch1") %>,
<%=request.getParameter("ch2") %>,
<%=request.getParameter("ch3") %>,
<%=request.getParameter("ch4") %>,
<%=request.getParameter("ch5") %>,
<%=request.getParameter("ch6") %>
<%}%>
<hr />
<%
if(request.getParameter("ch1")!=null){
int[] r = new int[6];
for(int i=0;i<6;i++){
 r[i]=(int) Math.floor(Math.random()*49+1); 
  for(int j=0;j<i;j++){//檢查有無重覆
    if(r[i]==r[j]){//有重覆重抽一次
      i--;
   break;
    }
  } 
} 
%>
開獎號碼: 
<%for(int i=0; i<r.length; i++){ %>
<%=r[i]%>, 
<%}}%>
</body>
</html>


random[i]=(int) Math.floor(Math.random()*49+1); 
此行程式隨機取1~49中的正整數。「*49」就是範圍值,「+1」就是起始值。
任何語言都有單字片語要記,若能記重點套句子或許能省點力


模擬兌獎

建立一個雙迴圈的結構,將所有的組合逐一比對。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Lotto</title>
</head>
<body>
	<%
	request.setCharacterEncoding("UTF-8");//將輸入編碼設定為UTF-8
	response.setCharacterEncoding("UTF-8");//將輸出編碼設定為UTF-8
	%>
    <form action="ch06.jsp" method="POST">
	    <input type="text" name="ch1" size="2" /> 
	    <input type="text" name="ch2" size="2" /> 
	    <input type="text" name="ch3" size="2" /> 
	    <input type="text" name="ch4" size="2" /> 
	    <input type="text" name="ch5" size="2" /> 
	    <input type="text" name="ch6" size="2" />
	    <button type="submit">確定</button>
    </form>
    <%if (request.getParameter("ch1") != null) {%>
    <hr />
	投注號碼:
    <%=request.getParameter("ch1")%>,
    <%=request.getParameter("ch2")%>,
    <%=request.getParameter("ch3")%>,
    <%=request.getParameter("ch4")%>,
    <%=request.getParameter("ch5")%>,
    <%=request.getParameter("ch6")%>
    <%}%>
    <hr />
    <%if(request.getParameter("ch1")!=null){
        int[] r = new int[6];              
        for (int i = 0; i < 6; i++) {
	        r[i] = (int) Math.floor(Math.random() * 49 + 1);
	        for (int j = 0; j < i; j++) {//檢查有無重覆
               if (r[i] == r[j]) {//有重覆重抽一次
                       i--;
                       break;
               }
	        }
        }
        %>
		開獎號碼:
        <%for (int i = 0; i < r.length; i++) {%>
        <%=r[i]%>,
        <%}%><hr/>
		中獎號碼: 
		<%
        if(request.getParameter("ch1")!=null)
        for(int i=0; i<5; i++){
	        for(int j=0; j<r.length; j++){
                if(Integer.parseInt(   request.getParameter("ch"+(i+1))) ==  r[j]   ){
                        out.print(r[j]+",");
                }
	        }
        }}
		%>
</body>
</html>


沒意外的話就這樣結束,之後不太會再去修改以上的設定。

套用裝飾器

前面建立的裝飾器「temp.jsp」複製一份名為「ch06.jsp」將內容區塊取代如下
<%
	request.setCharacterEncoding("UTF-8");//將輸入編碼設定為UTF-8
	response.setCharacterEncoding("UTF-8");//將輸出編碼設定為UTF-8
	%>
    <form action="ch06.jsp" method="POST">
	    <input type="text" name="ch1" style="width:80px;">
	    <input type="text" name="ch2" style="width:80px;">
	    <input type="text" name="ch3" style="width:80px;">
	    <input type="text" name="ch4" style="width:80px;">
	    <input type="text" name="ch5" style="width:80px;">
	    <input type="text" name="ch6" style="width:80px;"><br>
	    <button type="submit" class="btn btn-danger">開</button>
    </form>
    <%if (request.getParameter("ch1") != null && !request.getParameter("ch1").equals("")) {%>
    <hr />
	投注號碼:
    <%=request.getParameter("ch1")%>,
    <%=request.getParameter("ch2")%>,
    <%=request.getParameter("ch3")%>,
    <%=request.getParameter("ch4")%>,
    <%=request.getParameter("ch5")%>,
    <%=request.getParameter("ch6")%>    
    <hr />
    <%
        int[] r = new int[6];
        for (int i = 0; i < 6; i++) {
	        r[i] = (int) Math.floor(Math.random() * 49 + 1);
	        for (int j = 0; j < i; j++) {//檢查有無重覆
               if (r[i] == r[j]) {//有重覆重抽一次
                       i--;
                       break;
               }
	        }
        }
        %>
		開獎號碼:
        <%for (int i = 0; i < r.length; i++) {%>
        <%=r[i]%>,
        <%}%><hr/>
		中獎號碼:
		<%
        if(request.getParameter("ch1")!=null)
        for(int i=0; i<5; i++){
	        for(int j=0; j<r.length; j++){
                if(Integer.parseInt(   request.getParameter("ch"+(i+1))) ==  r[j]   ){
                        out.print(r[j]+",");
                }
	        }
        }
	}%>

置換內容

複製的文字中很容易找到「 內容區塊」4個字,選取這4個字置換程式碼即可套用飾器。複製的內容為本次內容中的body區塊,也就是將body標籤當中的內容複製出來貼上即可。「ch06.jsp」完整代碼如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@include file="conn.jsp"%><!-- 資料庫連線 -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title><!-- 瀏覽器標題 -->
<link href="style.css" rel="stylesheet">
<!-- 自定義裝飾 -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap-responsive.min.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
</head>
<body>
	<%@include file="menu.jsp"%><!-- 選單 -->
	<div class="container">		
		<div class="page-header">
			<h1>網頁標題</h1><!-- 網頁標題 -->
		</div>
	<%
	request.setCharacterEncoding("UTF-8");//將輸入編碼設定為UTF-8
	response.setCharacterEncoding("UTF-8");//將輸出編碼設定為UTF-8
	%>
    <form action="ch06.jsp" method="POST">
	    <input type="text" name="ch1" style="width:80px;">
	    <input type="text" name="ch2" style="width:80px;">
	    <input type="text" name="ch3" style="width:80px;">
	    <input type="text" name="ch4" style="width:80px;">
	    <input type="text" name="ch5" style="width:80px;">
	    <input type="text" name="ch6" style="width:80px;"><br>
	    <button type="submit" class="btn btn-danger">開</button>
    </form>
    <%if (request.getParameter("ch1") != null && !request.getParameter("ch1").equals("")) {%>
    <hr />
	投注號碼:
    <%=request.getParameter("ch1")%>,
    <%=request.getParameter("ch2")%>,
    <%=request.getParameter("ch3")%>,
    <%=request.getParameter("ch4")%>,
    <%=request.getParameter("ch5")%>,
    <%=request.getParameter("ch6")%>    
    <hr />
    <%
        int[] r = new int[6];
        for (int i = 0; i < 6; i++) {
	        r[i] = (int) Math.floor(Math.random() * 49 + 1);
	        for (int j = 0; j < i; j++) {//檢查有無重覆
               if (r[i] == r[j]) {//有重覆重抽一次
                       i--;
                       break;
               }
	        }
        }
        %>
		開獎號碼:
        <%for (int i = 0; i < r.length; i++) {%>
        <%=r[i]%>,
        <%}%><hr/>
		中獎號碼:
		<%
        if(request.getParameter("ch1")!=null)
        for(int i=0; i<5; i++){
	        for(int j=0; j<r.length; j++){
                if(Integer.parseInt(   request.getParameter("ch"+(i+1))) ==  r[j]   ){
                        out.print(r[j]+",");
                }
	        }
        }
	}%>
	</div>
	<%@include file="foot.jsp"%><!-- 頁尾 -->
</body>
</html>

沒有留言:

張貼留言