今天平安夜,协会需要做活动,第一个活动就是在群里发红包,第二个就是进协会网站的抽奖系统进行抽奖。
由于需要,所以我零时做了一个抽奖系统,是一种大转盘抽奖的。效果图如下:
这个基于Java抽奖的幸运大转盘抽奖系统是和微信的有点像,改编自一个网友的代码。
前台大概就是html+css+js,后台就是Java(实现的Java抽奖系统)。
后台Java代码为:
package com.ostarsier.servlet;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 抽奖概率算法来自:http://www.helloweba.com/view-blog-216.html
*/
public class AwardServlet extends HttpServlet {
private String ip;
private String name;
private static FileWriter out;
private static int ydj=1;//一等奖最多1名
private static int edj=2;//二等奖最多2名
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
ip=request.getParameter("ip");
name=request.getParameter("name");
Object[][] prizeArr = new Object[][]{
//里面的指针转动
{1,1,14,"一等奖",10},
{2,16,44,"不要灰心",10},
{3,46,74,"神马也没有",10},
{4,76,104,"祝您好运",10},
{5,106,134,"二等奖",15},
{6,136,164,"再接再厉",10},
{7,166,194,"神马也没有",10},
{8,196,224,"运气先攒着",10},
{9,226,254,"三等奖",20},
{10,256,284,"要加油哦",10},
{11,286,314,"神马也没有",10},
{12,316,344,"谢谢参与",10}
};
Object result[] = award(prizeArr);//抽奖后返回角度和奖品等级
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write("{\"angle\":\""+result[0]+"\",\"msg\":\""+result[2]+"\"}");
// System.out.println("转动角度:"+result[0]+"\t奖项ID:"+result[1]+"\t提示信息:"+result[2]);
}
//抽奖并返回角度和奖项
public Object[] award(Object[][] prizeArr){
//概率数组
Integer obj[] = new Integer[prizeArr.length];
for(int i=0;i0)
{
edj--;
}else
result=8;//二等奖名额没得了就强制给个3等的
}
return result;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
幸运大转盘抽奖
软件协会16年圣诞活动抽奖
备注:
同IP,姓名只能参加一次!
如果中奖了请把中奖信息截屏,直接发给会长。
一等:10元(最多1人)
二等:5元(最多2人)
三等:1元(人数无上限)