福利彩票双色球java
时间: 2023-12-08 21:39:19 浏览: 42
以下是福利彩票双色球Java的实现:
```java
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class DoubleColorBall {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入您要购买的彩票注数:");
int n = input.nextInt();
int[][] tickets = new int[n][7];
for (int i = 0; i < n; i++) {
System.out.println("请输入第" + (i + 1) + "注彩票的号码:");
for (int j = 0; j < 6; j++) {
System.out.print("请输入第" + (j + 1) + "个红球号码(1-33):");
int redBall = input.nextInt();
while (redBall < 1 || redBall > 33) {
System.out.print("输入有误,请重新输入第" + (j + 1) + "个红球号码(1-33):");
redBall = input.nextInt();
}
tickets[i][j] = redBall;
}
System.out.print("请输入蓝球号码(1-16):");
int blueBall = input.nextInt();
while (blueBall < 1 || blueBall > 16) {
System.out.print("输入有误,请重新输入蓝球号码(1-16):");
blueBall = input.nextInt();
}
tickets[i][6] = blueBall;
}
System.out.println("您购买的彩票号码为:");
for (int i = 0; i < n; i++) {
System.out.print("第" + (i + 1) + "注:");
for (int j = 0; j < 6; j++) {
System.out.print(tickets[i][j] + " ");
}
System.out.println("蓝球:" + tickets[i][6]);
}
int[] result = getResult();
System.out.println("本期开奖号码为:" + Arrays.toString(Arrays.copyOf(result, 6)) + " 蓝球:" + result[6]);
int totalMoney = n * 2;
int totalPrize = 0;
for (int i = 0; i < n; i++) {
int prize = getPrize(tickets[i], result);
totalPrize += prize;
if (prize > 0) {
System.out.println("第" + (i + 1) + "注彩票中奖了,中奖金额为:" + prize + "元");
}
}
System.out.println("您一共下注" + totalMoney + "元,累计中奖" + totalPrize + "元");
}
public static int[] getResult() {
int[] result = new int[7];
Random random = new Random();
for (int i = 0; i < 6; i++) {
int redBall = random.nextInt(33) + 1;
while (contains(result, redBall, i)) {
redBall = random.nextInt(33) + 1;
}
result[i] = redBall;
}
Arrays.sort(result, 0, 6);
int blueBall = random.nextInt(16) + 1;
result[6] = blueBall;
return result;
}
public static boolean contains(int[] array, int value, int length) {
for (int i = 0; i < length; i++) {
if (array[i] == value) {
return true;
}
}
return false;
}
public static int getPrize(int[] ticket, int[] result) {
int redCount = 0;
int blueCount = 0;
for (int i = 0; i < 6; i++) {
if (contains(result, ticket[i], 6)) {
redCount++;
}
}
if (ticket[6] == result[6]) {
blueCount = 1;
}
if (redCount == 6 && blueCount == 1) {
return 10000000;
} else if (redCount == 6) {
return 500000;
} else if (redCount == 5 && blueCount == 1) {
return 3000;
} else if (redCount == 5 || (redCount == 4 && blueCount == 1)) {
return 200;
} else if (redCount == 4 || (redCount == 3 && blueCount == 1)) {
return 10;
} else if (blueCount == 1) {
return 5;
} else {
return 0;
}
}
}
```