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> "); }else{ out.println(" <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>
时间: 2024-10-12 09:47:11