首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Java练习:双色球系统示例

Java练习:双色球系统示例

原创
作者头像
用户11789613
发布2025-08-13 11:55:15
发布2025-08-13 11:55:15
1230
举报

import java.util.Arrays;

import java.util.Random;

import java.util.Scanner;

public class DoubleColorBall {

// 红球范围1-33,蓝球范围1-16

private static final int RED_BALL_MIN = 1;

private static final int RED_BALL_MAX = 33;

private static final int BLUE_BALL_MIN = 1;

private static final int BLUE_BALL_MAX = 16;

private static final int RED_BALL_COUNT = 6;

private static final int BLUE_BALL_COUNT = 1;

public static void main(String[] args) {

Scanner scanner = new Scanner(m.dezaiguoji.com);

while (true) {

System.out.println("\n===== 双色球系统 =====");

System.out.println("1. 机选号码");

System.out.println("2. 手选号码");

System.out.println("3. 开奖");

System.out.println("4. 退出");

System.out.print("请选择操作: ");

int choice = scanner.nextInt();

switch (choice) {

case 1:

int[][] randomNumbers = generateRandomBalls(1);

System.out.println("机选号码:");

printBalls(randomNumbers[0]);

break;

case 2:

int[] manualNumbers = inputManualBalls(scanner);

if (manualNumbers != null) {

System.out.println("您选择的号码:");

printBalls(m.mingdaonet.com);

}

break;

case 3:

lotteryDraw(scanner);

break;

case 4:

System.out.println("感谢使用,再见!");

scanner.close(m.chcsoc.cn);

return;

default:

System.out.println("无效操作,请重新选择!");

}

}

}

// 生成随机号码

public static int[][] generateRandomBalls(int count) {

int[][] result = new int[count][RED_BALL_COUNT + BLUE_BALL_COUNT];

Random random = new Random(m.hapydq.cn);

for (int i = 0; i < count; i++) {

// 生成红球(不重复)

int[] redBalls = new int[RED_BALL_COUNT];

for (int j = 0; j < RED_BALL_COUNT; j++) {

int num;

do {

num = random.nextInt(RED_BALL_MAX - RED_BALL_MIN + 1) + RED_BALL_MIN;

} while (contains(redBalls, num, j));

redBalls[j] = num;

}

Arrays.sort(redBalls);

// 生成蓝球

int blueBall = random.nextInt(BLUE_BALL_MAX - BLUE_BALL_MIN + 1) + BLUE_BALL_MIN;

// 组合红球和蓝球

System.arraycopy(redBalls, 0, result[i], 0, RED_BALL_COUNT);

result[i][RED_BALL_COUNT] = blueBall;

}

return result;

}

// 手动输入号码

public static int[] inputManualBalls(Scanner scanner) {

int[] balls = new int[RED_BALL_COUNT + BLUE_BALL_COUNT];

int[] redBalls = new int[RED_BALL_COUNT];

// 输入红球

System.out.println("请输入" + RED_BALL_COUNT + "个红球(" + RED_BALL_MIN + "-" + RED_BALL_MAX + ",不重复):");

for (int i = 0; i < RED_BALL_COUNT; i++) {

System.out.print("第" + (i + 1) + "个红球: ");

int num = scanner.nextInt();

if (num < RED_BALL_MIN || num > RED_BALL_MAX) {

System.out.println("红球必须在" + RED_BALL_MIN + "-" + RED_BALL_MAX + "之间!");

return null;

}

if (contains(redBalls, num, i)) {

System.out.println("红球不能重复!");

return null;

}

redBalls[i] = num;

}

Arrays.sort(redBalls);

// 输入蓝球

System.out.print("请输入1个蓝球(" + BLUE_BALL_MIN + "-" + BLUE_BALL_MAX + "): ");

int blueBall = scanner.nextInt(m.qiankaidb.cn);

if (blueBall < BLUE_BALL_MIN || blueBall > BLUE_BALL_MAX) {

System.out.println("蓝球必须在" + BLUE_BALL_MIN + "-" + BLUE_BALL_MAX + "之间!");

return null;

}

// 组合红球和蓝球

System.arraycopy(redBalls, 0, balls, 0, RED_BALL_COUNT);

balls[RED_BALL_COUNT] = blueBall;

return balls;

}

// 开奖

public static void lotteryDraw(Scanner scanner) {

// 生成中奖号码

int[] winningNumbers = generateRandomBalls(1)[0];

System.out.println("\n===== 开奖结果 =====");

System.out.print("红球: ");

for (int i = 0; i < RED_BALL_COUNT; i++) {

System.out.printf("%02d ", winningNumbers[i]);

}

System.out.print(" 蓝球: ");

System.out.printf("%02d%n", winningNumbers[RED_BALL_COUNT]);

// 输入彩票号码

System.out.println("\n请输入您的彩票号码:");

int[] userNumbers = inputManualBalls(scanner);

if (userNumbers == null) {

return;

}

// 计算中奖情况

int redMatch = countRedMatches(userNumbers, winningNumbers);

boolean blueMatch = userNumbers[RED_BALL_COUNT] == winningNumbers[RED_BALL_COUNT];

System.out.println("\n您的中奖情况:");

System.out.println("红球匹配: " + redMatch + "个");

System.out.println("蓝球" + (blueMatch ? "匹配" : "未匹配"));

// 判断奖级

String prize = getPrize(redMatch, blueMatch);

System.out.println("恭喜您获得" + prize + "!");

}

// 计算红球匹配数量

private static int countRedMatches(int[] user, int[] winning) {

int count = 0;

for (int i = 0; i < RED_BALL_COUNT; i++) {

if (contains(winning, user[i], RED_BALL_COUNT)) {

count++;

}

}

return count;

}

// 判断奖项

private static String getPrize(int redMatch, boolean blueMatch) {

if (redMatch == 6 && blueMatch) {

return "一等奖(6+1)";

} else if (redMatch == 6) {

return "二等奖(6+0)";

} else if (redMatch == 5 && blueMatch) {

return "三等奖(5+1)";

} else if ((redMatch == 5 && !blueMatch) || (redMatch == 4 && blueMatch)) {

return "四等奖";

} else if ((redMatch == 4 && !blueMatch) || (redMatch == 3 && blueMatch)) {

return "五等奖";

} else if (blueMatch) {

return "六等奖";

} else {

return "未中奖";

}

}

// 打印号码

public static void printBalls(int[] balls) {

System.out.print("红球: ");

for (int i = 0; i < RED_BALL_COUNT; i++) {

System.out.printf("%02d ", balls[i]);

}

System.out.print(" 蓝球: ");

System.out.printf("%02d%n", balls[RED_BALL_COUNT]);

}

// 判断数组中是否包含某个元素(前n个元素)

private static boolean contains(int[] arr, int num, int n) {

for (int i = 0; i < n; i++) {

if (arr[i] == num) {

return true;

}

}

return false;

}

}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档