第三十二节( Java-实现体育彩票开奖系统)

DoubleBallServlet.java里:



package com.tanzhou.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tanzhou.util.DoubleBall;

public class DoubleBallServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public DoubleBallServlet() {
        super();
    }


    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
           doPost(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        DoubleBall db = new DoubleBall();
        String[] ball = db.doubleBall();
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>DoubleColorBall</TITLE></HEAD>");
        out.println("  <BODY style='text-align:center;'>");
        for(int i=0;i<ball.length;i++){
            if(i<6){
                out.println("<font style='color:red;font-weight:bold'>"+ball[i]+"</font>&nbsp;&nbsp;&nbsp;");
            }else{
                out.println("&nbsp;&nbsp;&nbsp;<font style='color:blue;font-weight:bold''>"+ball[i]+"</font>");
            }
            
        }
            
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    public void init() throws ServletException {
        // Put your code here
    }

}

////////////////////////////////////////////////////////////////////
DoubleBall.java里:

package com.tanzhou.util;

import java.util.Arrays;
import java.util.Random;

public class DoubleBall {
    
    public String[] doubleBall(){
        Random r = new Random(); // 产生随机数
        // 产生红球
        String[] balls = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33"};
        int ballsLen = balls.length;  // 33
        
        boolean[] used = new boolean[ballsLen]; // 标记位
        
        String[] ball = new String[6]; // 装载6红球的数组
        
        while(!checkNull(ball)){
            
            if(checkNull(ball)){   // 判断数组是否已经装满6个红球
                break;
            }
            
            // 取红球(只取6个红球)
            for(int i=0;i<ball.length;i++){
                int index = r.nextInt(ballsLen); // index =(0-32)  产生下标
                
                if(used[index]){ //已经取出来的就进入到下一次循环,不能取重复的数字
                    continue;
                }
                
                used[index] = true; // 将已经取出的红球下标标记为不可取
                ball[i] = balls[index];
                
                if(checkNull(ball)){   // 判断数组是否已经装满6个红球
                    break;
                }
            }
        }
        Arrays.sort(ball);
        System.out.println(Arrays.toString(ball));
        
        ball = Arrays.copyOf(ball, ball.length+1); // 将数组的内容复制到新的数组中
        
        // 取蓝球
        int index = r.nextInt(16);
        ball[ball.length-1] = balls[index];
        System.out.println(Arrays.toString(ball));
        
        return ball;
    }
    /**
     * 判断是否是否取满六个红球
     * @param ball
     * @return
     */
    public boolean checkNull(String[] ball){
        int num = 0 ;
        for(int k=0;k<ball.length;k++){
            if(ball[k]==null){
                num++;
            }
        }
        if(num==0){
            return true;
        }
        return false;
    }
    
    public static void main(String[] args){
        DoubleBall db = new DoubleBall();
        db.doubleBall();
    }
}

//////////////////////////////////////////////////////////////
web.xml文件里;


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <servlet>
    <servlet-name>DoubleBallServlet</servlet-name>
    <servlet-class>com.tanzhou.servlet.DoubleBallServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>DoubleBallServlet</servlet-name>
    <url-pattern>/DoubleColorBall</url-pattern>
  </servlet-mapping>

</web-app>

/////////////////////////////////////////////////////////////////


index.jsp文件里:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<script type="text/javascript">
    location.href = "${pageContext.request.contextPath}/DoubleColorBall";
</script>

 

posted @ 2015-02-08 10:14  哥的笑百度不到  阅读(370)  评论(0编辑  收藏  举报