java实现转盘抽奖;
index.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport"
content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<title>幸运大转盘抽奖</title>
<link href="files/activity-style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="files/jquery.min.js"></script>
<script type="text/javascript" src="files/jQueryRotate.2.2.js"></script>
<script type="text/javascript" src="files/jquery.easing.min.js"></script>
<script type="text/javascript">
$(function() {
$("#inner").click(function() {
lottery();
});
});
function lottery() {
$.ajax({
type : ‘POST‘,
url : ‘AwardServlet‘,
dataType : ‘json‘,
cache : false,
error : function() {
alert(‘出错了!‘);
return false;
},
success : function(json) {
$("#inner").unbind(‘click‘).css("cursor", "default");
var angle = parseInt(json.angle); //角度
var msg = json.msg; //提示信息
$("#outer").rotate({ //inner内部指针转动,outer外部转盘转动
duration : 5000, //转动时间
angle : 0, //开始角度
animateTo : 3600 + angle, //转动角度
easing : $.easing.easeOutSine, //动画扩展
callback : function() {
var con = confirm(msg + ‘\n还要再来一次吗?‘);
if (con) {
lottery();
} else {
return false;
}
}
});
}
});
}
</script>
</head>
<body class="activity-lottery-winning">
<div class="main">
<script type="text/javascript">
var loadingObj = new loading(document.getElementById(‘loading‘), {
radius : 20,
circleLineWidth : 8
});
loadingObj.show();
</script>
<div id="outercont">
<div id="outer-cont" style="overflow:hidden;">
<div id="outer">
<img src="files/activity-lottery-1.png" width="310px">
</div>
</div>
<div id="inner-cont">
<div id="inner">
<img src="files/activity-lottery-2.png">
</div>
</div>
</div>
</div>
</body>
</html>
AwardServlet.java
package com.ostarsier.servlet;
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 {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Object[][] prizeArr = new Object[][]{
//id,min,max,prize【奖项】,v【中奖率】
//外面的转盘转动
// {1,1,14,"一等奖",1},
// {2,346,364,"一等奖",1},
// {3,16,44,"不要灰心",10},
// {4,46,74,"神马也没有",10},
// {5,76,104,"祝您好运",10},
// {6,106,134,"二等奖",2},
// {7,136,164,"再接再厉",10},
// {8,166,194,"神马也没有",10},
// {9,196,224,"运气先攒着",10},
// {10,226,254,"三等奖",5},
// {11,256,284,"要加油哦",10},
// {12,286,314,"神马也没有",10},
// {13,316,344,"谢谢参与",10}
//里面的指针转动
{1,1,14,"一等奖",1},
{2,346,364,"一等奖",1},
{3,16,44,"不要灰心",10},
{4,46,74,"神马也没有",10},
{5,76,104,"祝您好运",10},
{6,106,134,"二等奖",2},
{7,136,164,"再接再厉",10},
{8,166,194,"神马也没有",10},
{9,196,224,"运气先攒着",10},
{10,226,254,"三等奖",5},
{11,256,284,"要加油哦",10},
{12,286,314,"神马也没有",10},
{13,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;i<prizeArr.length;i++){
obj[i] = (Integer) prizeArr[i][4];
}
Integer prizeId = getRand(obj); //根据概率获取奖项id
//旋转角度
int angle = new Random().nextInt((Integer)prizeArr[prizeId][2]-(Integer)prizeArr[prizeId][1])+(Integer)prizeArr[prizeId][1];
String msg = (String) prizeArr[prizeId][3];//提示信息
return new Object[]{angle,prizeId,msg};
}
//根据概率获取奖项
public Integer getRand(Integer obj[]){
Integer result = null;
try {
int sum = 0;//概率数组的总概率精度
for(int i=0;i<obj.length;i++){
sum+=obj[i];
}
for(int i=0;i<obj.length;i++){//概率数组循环
int randomNum = new Random().nextInt(sum);//随机生成1到sum的整数
if(randomNum<obj[i]){//中奖
result = i;
break;
}else{
sum -=obj[i];
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
具体代码:http://download.csdn.net/detail/mll999888/8578381
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。