大转盘抽奖java实现
时间: 2023-08-28 16:03:38 浏览: 92
java实现大转盘抽奖
4星 · 用户满意度95%
好的,以下是一个简单的大转盘抽奖的 Java 实现:
```java
import java.util.Random;
public class LuckyDraw {
private static final String[] PRIZES = {"一等奖", "二等奖", "三等奖", "四等奖", "五等奖", "六等奖", "七等奖", "八等奖", "谢谢参与"};
private static final int[] PROBABILITIES = {1, 2, 3, 4, 5, 6, 7, 8, 100};
private static final int TOTAL_PROBABILITY = 128;
public static void main(String[] args) {
int luckyNumber = new Random().nextInt(TOTAL_PROBABILITY);
int cumulativeProbability = 0;
int prizeIndex = 0;
for (int i = 0; i < PROBABILITIES.length; i++) {
cumulativeProbability += PROBABILITIES[i];
if (luckyNumber < cumulativeProbability) {
prizeIndex = i;
break;
}
}
System.out.println("您抽中的奖品是:" + PRIZES[prizeIndex]);
}
}
```
以上代码中,定义了奖品数组 `PRIZES` 和概率数组 `PROBABILITIES`,并且定义了总概率 `TOTAL_PROBABILITY`。通过 `Random` 类生成一个随机数作为抽奖的幸运数字,然后逐一累加每个奖品的概率,如果幸运数字小于当前累加概率,则抽中该奖品。最后输出抽中的奖品名称即可。
阅读全文