目录
题目要求
用java模拟双色球的投注和开奖
投注规则
双色球分红色球和蓝色球。红球一共6组,每组从[1,33]中抽取一个,六个互相不重复。蓝球从[1,16]中抽取一个数字,组成一注双色球选号。
开奖规则
需求分析
除主方法外,有以下三个关键方法
- 用户投注一组号码
- 随机生成一组中奖号码
- 判断中奖情况
还有一些辅助方法
- 检测红色球的重复(投注号码、生成中奖号码)
- 输出号码
- 输出庆祝语句
代码实现
import java.util.Scanner; //从键盘键入号码
import java.util.Random; //随机生成中奖号码
import java.util.Arrays; //排序数组
public class DoubleChromosphere {
public enum prizeLevel { //枚举奖项
FirstPrize, SecondPrize, ThirdPrize, FourthPrize, FifthPrize, SixthPrize, NoPrize
}
public static void main(String[] args) {
System.out.println("————请投注号码————");
int[] userNumbers = selectUserNumbers();
System.out.println("————投注结束————");
System.out.println("以下是您投注的号码: ");
output(userNumbers);
System.out.print("\n");
int[] winningNumbers = createWinningNumbers();
System.out.println("以下是开奖号码: ");
output(winningNumbers);
prizeLevel userPrizeLevel = judge(userNumbers, winningNumbers);
congratulations(userPrizeLevel);
}
/**
* 用于用户投注号码
* @return 用户选择的号码数组
*/
public static int[] selectUserNumbers() {
Scanner sc = new Scanner(System.in);
int[] userNum = new int[7];
boolean succeed; //用于检测是否投注成功
for (int i = 0; i < 6; i++) {
int redTemp;
do {
System.out.print("请输入您选择的第" + (i + 1) + "个红色球号码:");
succeed = true;
redTemp = sc.nextInt();
if (redTemp < 1 || redTemp > 33) {
System.out.println("您选择的号码超出范围了,请重新输入!");
succeed = false;
} else if (repeat(redTemp, userNum)) {
System.out.println("您已经选过该号码了,请重新输入!");
succeed = false;
}
} while (!succeed);
userNum[i] = redTemp;
}
int blueTemp;
do {
System.out.print("请输入您选择的蓝色球号码:");
blueTemp = sc.nextInt();
if (blueTemp < 1 || blueTemp > 16) {
System.out.println("您选择的号码超出范围了,请重新输入!");
succeed = false;
} else {
succeed = true;
}
} while (!succeed);
userNum[6] = blueTemp;
return userNum;
}
/**
* 用于随机生成一组中奖号码
* @return 中奖号码数组
*/
public static int[] createWinningNumbers() {
int[] luckyNum = new int[7];
Random r = new Random();
for (int i = 0; i < 6; i++) {
int temp;
do {
temp = r.nextInt(33) + 1; //随机生成0至32的数,再加1
} while (repeat(temp, luckyNum));
luckyNum[i] = temp;
}
luckyNum[6] = r.nextInt(16) + 1;
return luckyNum;
}
/**
* 用于判断用户投注的情况,返回中奖等级
* @param wager 用户投注号码数组
* @param lottery 中奖号码数组
* @return 中奖等级
*/
public static prizeLevel judge(int[] wager, int[] lottery) {
int countRed = 0; //countRed记录用户中了多少个红球
for (int i = 0; i < 6; i++) {
if (repeat(wager[i], lottery)) {
countRed++;
}
}
boolean ifBlue = wager[6] == lottery[6]; //countBlue记录用户是否中蓝球
if (countRed == 6 && ifBlue) {
return prizeLevel.FirstPrize;
} else if (countRed == 6) {
return prizeLevel.SecondPrize;
} else if (countRed == 5 && ifBlue) {
return prizeLevel.ThirdPrize;
} else if (countRed == 5 || countRed == 4 && ifBlue) {
return prizeLevel.FourthPrize;
} else if (countRed == 4 || countRed == 3 && ifBlue) {
return prizeLevel.FifthPrize;
} else if (ifBlue) {
return prizeLevel.SixthPrize;
} else {
return prizeLevel.NoPrize;
}
}
/**
* 检测红色球的重复,用于判断是否重复下注或红色球是否中奖
* @param num 待检测的号码
* @param arr 待检测的数组
* @return 是否重复
*/
public static boolean repeat(int num, int[] arr) {
for (int i = 0; i < 6; i++) { // 只检查前6个红球号码
if (num == arr[i]) {
return true;
}
}
return false;
}
/**
* 用于输出号码
* @param outputArray 待输出的号码数组
*/
public static void output(int[] outputArray) {
int[] redBall = Arrays.copyOfRange(outputArray, 0, 6);
Arrays.sort(redBall); //对红色球数组按升序排序
System.out.print("红色球:");
for (int element : redBall) {
System.out.print(element + " ");
}
System.out.print("蓝色球:" + outputArray[6]);
}
/**
* 用于打印庆祝语句"恭喜获得x等奖"
* @param level 中奖等级
*/
public static void congratulations(prizeLevel level) {
System.out.print("\n");
switch (level) {
case NoPrize -> System.out.println("很遗憾,您没有获奖。");
case SixthPrize -> System.out.println("恭喜,您获得了六等奖!");
case FifthPrize -> System.out.println("恭喜,您获得了五等奖!");
case FourthPrize -> System.out.println("恭喜,您获得了四等奖!");
case ThirdPrize -> System.out.println("恭喜,您获得了三等奖!");
case SecondPrize -> System.out.println("恭喜,您获得了二等奖!");
case FirstPrize -> System.out.println("恭喜,您获得了一等奖!");
}
}
}
运行预览
以上即为利用java实现双色球模拟的代码,如有任何错漏、不足、可简化提升之处,敬请批评指正!
2024年4月6日
标签:双色球,Java,号码,int,System,println,return,模拟,out From: https://blog.csdn.net/ouoi330/article/details/137337374