第三十二节( 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("");
        out.println("");
        out.println("  DoubleColorBall");
        out.println("  ");
        for(int i=0;i){
            if(i<6){
                out.println(""+ball[i]+"   ");
            }else{
                out.println("   "+ball[i]+"");
            }
            
        }
            
        out.println("  ");
        out.println("");
        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){
                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){
            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文件里;


"1.0" encoding="UTF-8"?>
"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">
  
    DoubleBallServlet
    class>com.tanzhou.servlet.DoubleBallServletclass>
  

  
    DoubleBallServlet
    /DoubleColorBall
  



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


index.jsp文件里:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

 

转载于:https://www.cnblogs.com/Deng1185246160/p/4279734.html

你可能感兴趣的:(第三十二节( Java-实现体育彩票开奖系统))