Java implementation turntable lottery;

Source: Internet
Author: User

index.jsp page:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://"
+ request.getservername () + ":" + request.getserverport ()
+ path + "/";
%>
<! DOCTYPE html>
<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> Lucky Big Wheel Draw </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 (' Something went wrong! ‘);
return false;
},

Success:function (JSON) {
$ ("#inner"). Unbind (' click '). CSS ("cursor", "Default");
var angle = parseint (Json.angle); Angle
var msg = json.msg; Prompt information
$ ("#outer"). Rotate ({//inner internal pointer rotation, outer external turntable rotation
duration:5000,//rotation time
angle:0,//Start angle
animateto:3600 + angle,//rotation angle
Easing: $.easing.easeoutsine,//animation extension
Callback:function () {
var con = confirm (msg + ' \ n Do you want to do it again? ‘);
if (Con) {
Lottery ();
} else {
return false;
}
}
});
}
});
}
</script>

<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" >

</div>
</div>
<div id= "Inner-cont" >
<div id= "inner" >

</div>
</div>
</div>
</div>

</body>



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;
/**
* Lottery probability algorithm from: 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 "award", V "winning rate"
The turntable outside is turning
{1,1,14, "First Prize", 1},
{2,346,364, "First Prize", 1},
{3,16,44, "Don't Lose Heart", 10},
{4,46,74, "Neither God nor horse", 10},
{5, 76,104, "Good Luck", 10},
{6,106,134, "Second Prize", 2},
{7,136,164, "persistent", 10},
{8,166,194, "No Gods and Horses", 10},
{9,196,224, "Fortune First Saved", 10},
{10,226,254, "Third Prize", 5},
{11,256,284, "to refuel Oh", 10},
{12,286,314, "No Gods and Horses", 10},
{13,316,344, "Thank you for participating", 10}

The inside of the pointer rotates
{1,1,14, "First Prize", 1},
{2,346,364, "First Prize", 1},
{3,16,44, "Don't Lose Heart", 10},
{4,46,74, "Neither God nor horse", 10},
{5, 76,104, "Good Luck", 10},
{6,106,134, "Second Prize", 2},
{7,136,164, "persistent", 10},
{8,166,194, "No Gods and Horses", 10},
{9,196,224, "Fortune First Saved", 10},
{10,226,254, "Third Prize", 5},
{11,256,284, "to refuel Oh", 10},
{12,286,314, "No Gods and Horses", 10},
{13,316,344, "Thank you for participating", 10}
};
Object result[] = Award (Prizearr);//return angle and prize level after draw
Response.setcontenttype ("Text/html;charset=utf-8");
Response.getwriter (). Write ("{\" angle\ ": \" "+result[0]+" \ ", \" msg\ ": \" "+result[2]+" \ "}");
System.out.println ("Rotation angle:" +result[0]+ "\ T Prize ID:" +result[1]+ "\ t prompt message:" +result[2]);
}

Draw and return to angles and awards
Public object[] Award (object[][] Prizearr) {
Probability array
Integer obj[] = new Integer[prizearr.length];
for (int i=0;i<prizearr.length;i++) {
Obj[i] = (Integer) prizearr[i][4];
}
Integer Prizeid = Getrand (obj); Get the prize ID based on probability
Rotation angle
int angle = new Random (). Nextint ((integer) prizearr[prizeid][2]-(integer) prizearr[prizeid][1]) + (integer) prizearr[ PRIZEID][1];
String msg = (string) prizearr[prizeid][3];//hint information
return new object[]{angle,prizeid,msg};
}
Win prizes based on probabilities
Public integer Getrand (integer obj[]) {
Integer result = null;
try {
int sum = Total probability accuracy of 0;//probability array
for (int i=0;i<obj.length;i++) {
Sum+=obj[i];
}
for (int i=0;i<obj.length;i++) {//probability array loop
int randomnum = new Random (). Nextint (sum);//randomly generate an integer of 1 to sum
if (Randomnum<obj[i]) {//Winning
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);
}

}
Specific code: http://download.csdn.net/detail/mll999888/8578381

Java implementation Carousel Lottery;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.