用java写一个双色球项目
时间: 2023-03-06 21:44:23 浏览: 88
基于java+mysql实现双色球购买及抽奖程序控制台版本(完整源码+数据库+面向对象)
好的,以下是一个简单的Java双色球项目示例代码:
```java
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class DoubleColorBall {
public static void main(String[] args) {
int[] redBalls = new int[6];
int blueBall;
// 随机生成6个红球号码
Random rand = new Random();
for (int i = 0; i < 6; i++) {
redBalls[i] = rand.nextInt(33) + 1;
for (int j = 0; j < i; j++) {
if (redBalls[i] == redBalls[j]) {
i--;
break;
}
}
}
Arrays.sort(redBalls);
// 随机生成一个蓝球号码
blueBall = rand.nextInt(16) + 1;
// 提示用户输入双色球号码
Scanner input = new Scanner(System.in);
System.out.print("请输入红球号码(6个数字,用空格隔开):");
int[] userRedBalls = new int[6];
for (int i = 0; i < 6; i++) {
userRedBalls[i] = input.nextInt();
}
Arrays.sort(userRedBalls);
System.out.print("请输入蓝球号码:");
int userBlueBall = input.nextInt();
// 判断中奖情况
int redCount = 0;
for (int i = 0; i < 6; i++) {
if (userRedBalls[i] == redBalls[i]) {
redCount++;
}
}
boolean blueMatched = (userBlueBall == blueBall);
int prize = 0;
if (redCount == 6 && blueMatched) {
prize = 1; // 一等奖
} else if (redCount == 6 && !blueMatched) {
prize = 2; // 二等奖
} else if (redCount == 5 && blueMatched) {
prize = 3; // 三等奖
} else if (redCount == 4 && blueMatched) {
prize = 4; // 四等奖
} else if (redCount == 3 && blueMatched) {
prize = 5; // 五等奖
} else if ((redCount == 5 && !blueMatched) || (redCount == 4 && !blueMatched)) {
prize = 6; // 六等奖
}
// 输出中奖结果
System.out.println("中奖号码为:" + Arrays.toString(redBalls) + " + " + blueBall);
System.out.println("用户输入的号码为:" + Arrays.toString(userRedBalls) + " + " + userBlueBall);
if (prize > 0) {
System.out.println("恭喜您中了" + prize + "等奖!");
} else {
System.out.println("很遗憾,您没有中奖。");
}
}
}
```
该程序首先随机生成双色球开奖号码,然后提示用户输入号码,并判断用户的号码是否中奖。根据中
阅读全文