java代码抽奖
2016-09-02 14:18:49 qazwsx081 阅读数 1290
`<pre name="code" class="plain">`

`奖品实体类如下：`
```import java.math.BigDecimal;

public class Prize {

/**
* 奖品唯一标示
*/
private Integer prizeId;

/**
* 中奖概率
*/
private BigDecimal probability;

/**
* 奖品数量
*/
private Integer quantity;

public Integer getPrizeId() {
return prizeId;
}

public void setPrizeId(Integer prizeId) {
this.prizeId = prizeId;
}

public BigDecimal getProbability() {
return probability;
}

public void setProbability(BigDecimal probability) {
this.probability = probability;
}

public Integer getQuantity() {
return quantity;
}

public void setQuantity(Integer quantity) {
this.quantity = quantity;
}

}
```
`抽奖关键方法`
```import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;

public class Arithmetic {
// 放大倍数
private static final int mulriple = 1000;

public int pay(List<Prize> prizes) {
int lastScope = 0;
// 洗牌，打乱奖品次序
Collections.shuffle(prizes);
Map<Integer, int[]> prizeScopes = new HashMap<Integer, int[]>();
Map<Integer, Integer> prizeQuantity = new HashMap<Integer, Integer>();
for (Prize prize : prizes) {
int prizeId = prize.getPrizeId();
// 划分区间
int currentScope = lastScope + prize.getProbability().multiply(new BigDecimal(mulriple)).intValue();
prizeScopes.put(prizeId, new int[] { lastScope + 1, currentScope });
prizeQuantity.put(prizeId, prize.getQuantity());

lastScope = currentScope;
}

// 获取1-1000000之间的一个随机数
int luckyNumber = new Random().nextInt(mulriple);
int luckyPrizeId = 0;
// 查找随机数所在的区间
if ((null != prizeScopes) && !prizeScopes.isEmpty()) {
Set<Entry<Integer, int[]>> entrySets = prizeScopes.entrySet();
for (Map.Entry<Integer, int[]> m : entrySets) {
int key = m.getKey();
if (luckyNumber >= m.getValue()[0] && luckyNumber <= m.getValue()[1] && prizeQuantity.get(key) > 0) {
luckyPrizeId = key;
break;
}
}
}

if (luckyPrizeId > 0) {
// 奖品库存减一
}

return luckyPrizeId;
}
}
```
`测试抽奖的main方法`
```import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.meidaojia.common.util.OrmUtil;

public class Test {
public static void main(String[] args) throws IOException {
List<Prize> prizes = new ArrayList<Prize>();
Prize prize1 = new Prize();
prize1.setPrizeId(1);
prize1.setProbability(new BigDecimal(0.00));
prize1.setQuantity(1);

Prize prize2 = new Prize();
prize2.setPrizeId(2);
prize2.setProbability(new BigDecimal(0.10));
prize2.setQuantity(10);

Prize prize3 = new Prize();
prize3.setPrizeId(3);
prize3.setProbability(new BigDecimal(0.50));
prize3.setQuantity(20);

Prize prize4 = new Prize();
prize4.setPrizeId(4);
prize4.setProbability(new BigDecimal(0.20));
prize4.setQuantity(3);

Prize prize5 = new Prize();
prize5.setPrizeId(5);
prize5.setProbability(new BigDecimal(0.00));
prize5.setQuantity(200);

int prize1GetTimes = 0;
int prize2GetTimes = 0;
int prize3GetTimes = 0;
int prize4GetTimes = 0;
int prize5GetTimes = 0;
Arithmetic arithmetic = new Arithmetic();
int times = 10000;
for (int i = 0; i < times; i++) {
int prizeId = arithmetic.pay(prizes);
switch (prizeId) {
case 1:
prize1GetTimes++;
break;
case 2:
prize2GetTimes++;
break;
case 3:
prize3GetTimes++;
break;
case 4:
prize4GetTimes++;
break;
case 5:
prize5GetTimes++;
break;
}
}
System.out.println("抽奖次数" + times);
System.out.println("prize1中奖次数" + prize1GetTimes);
System.out.println("prize2中奖次数" + prize2GetTimes);
System.out.println("prize3中奖次数" + prize3GetTimes);
System.out.println("prize4中奖次数" + prize4GetTimes);
System.out.println("prize5中奖次数" + prize5GetTimes);

}
}```

2017-12-06 16:51:07 yfinhere 阅读数 339
```public class SpringBootDemo31Application {

/**
* 一等奖：比例（percentOne）10/100
* 二等奖：比例（percentTwo）20/100
* 三等奖：比例（percentThree）30/100
* 四等奖：比例（percentFour）40/100
*
* @param args
*/
public static void main(String[] args) {

String percentOne="10/100";
String percentTwo="20/100";
String percentThree="30/100";
String percentFour="40/100";
int weightOne=getWeight(percentOne);
int weightTwo=getWeight(percentTwo);
int weightThree=getWeight(percentThree);
int weightFour=getWeight(percentFour);
int weightTotal=weightOne+weightTwo+weightThree+weightFour;
double b1=(double) weightOne/weightTotal;
double b2=b1+(double) weightTwo/weightTotal;
double b3=b2+(double) weightThree/weightTotal;
double b4=b3+(double) weightFour/weightTotal;
int i1=0,i2=0,i3=0,i4=0,i5=0;
for(int t=0;t<1000;t++) {
double randomN = Math.random();
String result = "恭喜您获得";
if (randomN <= b1) {
result += "一等奖";
i1++;
} else if (b1 < randomN && randomN <= b2) {
result += "二等奖";
i2++;
} else if (b2 < randomN && randomN <= b3) {
result += "三等奖";
i3++;
} else if (b3 < randomN && randomN <= b4) {
result += "四等奖";
i4++;
} else {
result = "没有中奖";
i5++;
}
//System.out.println(result);
}
System.out.println(i1+"      "+i2+"         "+i3+"      "+i4+"         "+i5);
System.out.println((double)i1/(i1+i2+i3+i4));
System.out.println((double)i2/(i1+i2+i3+i4));
System.out.println((double)i3/(i1+i2+i3+i4));
System.out.println((double)i4/(i1+i2+i3+i4));

}

public static int getWeight(String percent){
String[] splits=percent.split("/");
return Integer.parseInt(splits[0]);
}```
2016-09-14 17:01:21 qq_36074362 阅读数 1074
```<span style="font-size:18px;">Scanner input = new Scanner(System.in);
String answer = "y";
String mima = "";
String name = "";
int max = 9999;
int min = 1000;
int hao = 0;
do {
System.out.println("***********欢迎进入客奖富翁系统**************");
System.out.println("\t1.注册");
System.out.println("\t2.登录");
System.out.println("\t3.抽奖");
System.out.println("请选择菜单:");
int chioce = input.nextInt();
switch (chioce) {
case 1:
System.out.println("奖客富翁系统 > 注册");
System.out.println("请填写个人信息:");
System.out.println("用户名:");
name = input.next();
System.out.println("密码:");
mima = input.next();
hao = (int) (Math.random() * (max - min)) + min;
System.out.println("注册成功,请记好你的会员号!");
System.out.println("用户名\t密码\t会员卡号");
System.out.println(name + "\t" + mima + "\t" + hao);
break;
case 2:
System.out.println("奖客富翁系统 > 登录");
int k;
for (k = 2; k >= 0; k--) {
System.out.println("请输入用户名:");
String name1 = input.next();
System.out.println("请输入密码:");
String mima1 = input.next();
if (name.equals(name1) && mima.equals(mima1)) {
System.out.println("欢迎你:" + name1);
} else {
System.out.println("你还有" + k + "次机会");
continue;
}
}
if (k < 0) {
System.out.println("您的三次均输入错误");
}
break;
case 3:
System.out.println("奖客富翁系统 > 抽奖");
System.out.println("请输入你的卡号:");
hao = input.nextInt();
int i;
int[] shu = new int[5];
for (i = 0; i < shu.length; i++) {
shu[i] = (int) (Math.random() * (max - min)) + min;
}
System.out.print("\n本日的幸运数字为：");
for (i = 0; i < shu.length; i++) {
System.out.print(shu[i] + "  ");
}
for (i = 0; i < shu.length; i++) {
if (shu[i] == hao) {
System.out.println("\n恭喜！您是本日的幸运会员！");
break;
}
}
if (i == shu.length) {
System.out.println("\n抱歉！您不是本日的幸运会员！");
}
break;
default:
System.out.println("您的输入有误！");
break;
}
System.out.println("继续吗?");
System.out.println("系统退出,谢谢使用!");
}</span>```

2012-09-28 14:22:00 zhanglu0223 阅读数 59

```package com.luiszhang.test;

import java.util.Arrays;

/**
* NumberLotteryGame
* 一个简单的数字彩票游戏类
* @author LuisZhang
* 参考了core java 8th中的例3-7的设计思想
*/
public class NumberLotteryGame {
private int gamesNumber;    // 生成游戏的数量，为以后多线程扩展做考虑
private int numbersLength;  // 数字序列的总长度
private int winningNumbersLength;  // 中奖的数字序列的长度

/**
* Constructor with three parameters
* 初始化3个参数的构造方法
* @param gamesNumber
* @param numbersLength
* @param winningNumbersLength
*/
public NumberLotteryGame(int gamesNumber, int numbersLength, int winningNumbersLength) {
this.gamesNumber = gamesNumber;
this.numbersLength = numbersLength;
this.winningNumbersLength = winningNumbersLength;
}

/**
* Generate a number Array
* 生成一个产生中奖序列所需的数字序列
* @param length 数字序列的长度
* @return numbers 产生的数字序列
*/
public static int[] generateNumberArray(int length) {
int[] numbers = new int[length];

for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1;
}

return numbers;
}

/**
* Generate a winning number array
* 根据生成的数字序列，产生不重复的中奖数字序列
* @param length 中奖数字序列的长度
* @return result 中奖数字数组
*/
public int[] generateWinningNumberArray(int length) {
int[] numbers = NumberLotteryGame.generateNumberArray(numbersLength);

int[] result = new int[length];

int n = numbersLength;

// 该for循环为产生不重复的中奖序列的核心代码
for(int i = 0; i < result.length; i++) {
int r = (int) (Math.random() * n);  // 随机产生一个从0——(n-1)的数字,Math.random()
// 随机产生一个[0, 1)范围的double型数值,
result[i] = numbers[r];             // 将该随机数字作为数组的下标，
// 将该下标对应的值赋给result[i]
numbers[r] = numbers[n - 1];        // 将numbers数组的numbers[n-1]的值，赋给刚已赋
// 值过的numbers[r]。
n--;   // 将n-1,从而下一次循环产生的随机的原数组下标的范围从0——（n-1）-1,
// 保证了上一步中，已经赋值给数组中其他数的numbers[n-1]，不会在下次循环中给取
// 得，从而保证了产生的中奖数组result为不重复的。
}

return result;
}

/**
* Show winning NumberArray
* 显示中将数组
*/
public void showWinningNumberArray() {
int[] winningNumbers = this.generateWinningNumberArray(winningNumbersLength);
Arrays.sort(winningNumbers);

for(int r : winningNumbers) {
System.out.print(r + " ");
}

System.out.println();
}

/**
* @return the gamesNumber
*/
public int getGamesNumber() {
return gamesNumber;
}

/**
* @param gamesNumber the gamesNumber to set
*/
public void setGamesNumber(int gamesNumber) {
this.gamesNumber = gamesNumber;
}

/**
* @return the numbersLength
*/
public int getNumbersLength() {
return numbersLength;
}

/**
* @param numbersLength the numbersLength to set
*/
public void setNumbersLength(int numbersLength) {
this.numbersLength = numbersLength;
}

/**
* @return the winningNumbersLength
*/
public int getWinningNumbersLength() {
return winningNumbersLength;
}

/**
* @param winningNumbersLength the winningNumbersLength to set
*/
public void setWinningNumbersLength(int winningNumbersLength) {
this.winningNumbersLength = winningNumbersLength;
}

/**
* Main method
* 用于测试该类的main方法
* @param args
*/
public static void main(String[] args) {
int i = 20;    // int i = Integer.parseInt(args[0]);
for(int j = 0; j < i; j++) {
NumberLotteryGame game1 = new NumberLotteryGame(1, 15, 7);
game1.showWinningNumberArray();
}
}
}```

2012-09-28 14:22:42 zhanglu0223 阅读数 4865

最近，正在看《core java 8th》这本书。自己按照书上作者一段代码的思想，自己动手写了一个小代码，深刻的感觉到作者Horstmann超高的代码思想。《core java 8th》真的确实是一本好书，值得慢慢品读，会给人带来无穷的惊喜。自己写的代码与注释如下，废话不多说，上代码。

```package com.luiszhang.test;

import java.util.Arrays;

/**
* NumberLotteryGame
* 一个简单的数字彩票游戏类
* @author LuisZhang
* 参考了core java 8th中的例3-7的设计思想
*/
public class NumberLotteryGame {
private int gamesNumber;    // 生成游戏的数量，为以后多线程扩展做考虑
private int numbersLength;  // 数字序列的总长度
private int winningNumbersLength;  // 中奖的数字序列的长度

/**
* Constructor with three parameters
* 初始化3个参数的构造方法
* @param gamesNumber
* @param numbersLength
* @param winningNumbersLength
*/
public NumberLotteryGame(int gamesNumber, int numbersLength, int winningNumbersLength) {
this.gamesNumber = gamesNumber;
this.numbersLength = numbersLength;
this.winningNumbersLength = winningNumbersLength;
}

/**
* Generate a number Array
* 生成一个产生中奖序列所需的数字序列
* @param length 数字序列的长度
* @return numbers 产生的数字序列
*/
public static int[] generateNumberArray(int length) {
int[] numbers = new int[length];

for (int i = 0; i < numbers.length; i++) {
numbers[i] = i + 1;
}

return numbers;
}

/**
* Generate a winning number array
* 根据生成的数字序列，产生不重复的中奖数字序列
* @param length 中奖数字序列的长度
* @return result 中奖数字数组
*/
public int[] generateWinningNumberArray(int length) {
int[] numbers = NumberLotteryGame.generateNumberArray(numbersLength);

int[] result = new int[length];

int n = numbersLength;

// 该for循环为产生不重复的中奖序列的核心代码
for(int i = 0; i < result.length; i++) {
int r = (int) (Math.random() * n);  // 随机产生一个从0——(n-1)的数字,Math.random()
// 随机产生一个[0, 1)范围的double型数值,
result[i] = numbers[r];             // 将该随机数字作为数组的下标，
// 将该下标对应的值赋给result[i]
numbers[r] = numbers[n - 1];        // 将numbers数组的numbers[n-1]的值，赋给刚已赋
// 值过的numbers[r]。
n--;   // 将n-1,从而下一次循环产生的随机的原数组下标的范围从0——（n-1）-1,
// 保证了上一步中，已经赋值给数组中其他数的numbers[n-1]，不会在下次循环中给取
// 得，从而保证了产生的中奖数组result为不重复的。
}

return result;
}

/**
* Show winning NumberArray
* 显示中将数组
*/
public void showWinningNumberArray() {
int[] winningNumbers = this.generateWinningNumberArray(winningNumbersLength);
Arrays.sort(winningNumbers);

for(int r : winningNumbers) {
System.out.print(r + " ");
}

System.out.println();
}

/**
* @return the gamesNumber
*/
public int getGamesNumber() {
return gamesNumber;
}

/**
* @param gamesNumber the gamesNumber to set
*/
public void setGamesNumber(int gamesNumber) {
this.gamesNumber = gamesNumber;
}

/**
* @return the numbersLength
*/
public int getNumbersLength() {
return numbersLength;
}

/**
* @param numbersLength the numbersLength to set
*/
public void setNumbersLength(int numbersLength) {
this.numbersLength = numbersLength;
}

/**
* @return the winningNumbersLength
*/
public int getWinningNumbersLength() {
return winningNumbersLength;
}

/**
* @param winningNumbersLength the winningNumbersLength to set
*/
public void setWinningNumbersLength(int winningNumbersLength) {
this.winningNumbersLength = winningNumbersLength;
}

/**
* Main method
* 用于测试该类的main方法
* @param args
*/
public static void main(String[] args) {
int i = 20;    // int i = Integer.parseInt(args[0]);
for(int j = 0; j < i; j++) {
NumberLotteryGame game1 = new NumberLotteryGame(1, 15, 7);
game1.showWinningNumberArray();
}
}
}```

Java 每日抽奖

Java抽奖游戏

importjava.io.*;classKY5_3{publicstaticvoidmain(Stringargs[])throwsIOException{charch;System.out.println("抽奖游戏，祝您好运!");Syste...

java抽奖详细介绍

java抽奖系统

java抽奖系统

java 抽奖算法