利用Java实现福彩双色球项目(支持单式/复式购买)
买彩票判断是否中奖,中奖后判断是几等奖
双色球投注区分为红色球号码区和蓝色球号码区,
红色球号码区由1-33共33个号码组成,蓝色球号码区由1-16共16个号码组成。
投注时选择6个红球号码和1个蓝球号码组成一注进行单式投注,每注金额2元。
一等奖:投注号码与当期开奖号码全部相同(顺序不限,下同),即中奖;
二等奖:投注号码与当期开奖号码中的6个红色球号码相同,即中奖;
三等奖:投注号码与当期开奖号码中的任意5个红色球号码和1个蓝色球号码相同,即中奖;
四等奖:投注号码与当期开奖号码中的任意5个红色球号码相同,或与任意4个红色球号码和1个蓝色球号码相同,即中奖;
五等奖:投注号码与当期开奖号码中的任意4个红色球号码相同,或与任意3个红色球号码和1个蓝色球号码相同,即中奖;
六等奖:投注号码与当期开奖号码中的1个蓝色球号码相同,即中奖。
显示中奖结果的同时显示您一共下注人民币??元,累计中奖人民币??元
package cn.mychenwei.lottery;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author 陈小哥cw
* @qq 1322311203
* @时间 2019-03-23 17:55:06
*/
public class TestDemo {
//存储红色球
static int[] red = new int[6];
//存储蓝色球
static int blue;
//判断是否购买彩票
static boolean bool;
//彩票每注2元
static int price = 2;
//存储彩票下注数量
static int count;
//存储复式红色球号数
static int[][] reds = new int[1000][6];
//存储复式蓝色球号数
static int[] blues = new int[1000];
//存储彩票下注方式(0为复式,1为单式,默认为复式)
static int type;
static Scanner sc = new Scanner(System.in);
//存储中奖红色球号
static int[] redReal = new int[6];
//存储中奖蓝色球号
static int blueReal;
public static void main(String[] args) {
menu();
}
//主界面
public static void menu() {
System.out.println("******欢迎进入双色球彩票系统******");
System.out.println("\t\t1,购买彩票");
System.out.println("\t\t2,查看开奖");
System.out.println("\t\t3,退出");
System.out.println("*********************************");
System.out.print("请选择菜单:");
int num = sc.nextInt();
switch (num) {
case 1:
buy();
menu();
break;
case 2:
if (bool) {
show();
} else {
System.out.println("您还未购买彩票哦!请先购买");
}
menu();
break;
case 3:
System.out.println("谢谢使用本系统,欢迎下次再见!");
return;
default:
System.out.println("请输入正确序号(1-3)哦!");
menu();
}
}
//购买彩票
public static void buy() {
bool = true;
System.out.println("\n双色球彩票系统>>>>>>>>>购买彩票\n");
System.out.print("请选择彩票购买方式(单式/复式)(D/F d/f):");
char[] c = sc.next().toCharArray();
switch (c[0]) {
case 'd':
case 'D':
buySingle();
break;
case 'f':
case 'F':
buyDuplicate();
break;
}
}
//单式购买双色球(每注不同)
public static void buySingle() {
type = 1;//单式购买
System.out.print("您需要下多少注?请输入:");
count = sc.nextInt();
System.out.println("是否让机器自动选择号数?请输入Y/N y/n");
char[] c = sc.next().toCharArray();
switch (c[0]) {
case 'y':
case 'Y': {
for (int k = 0; k < count; k++) {
reds[k][0] = (int) (Math.random() * 33 + 1);
for (int i = 1; i < reds[k].length; i++) {
//生成的开奖号码与前者不能相同
reds[k][i] = (int) (Math.random() * 33 + 1);
for (int j = 0; j < i; j++) {
if (reds[k][i] == reds[k][j]) {
i--;//如果重复 i--相当于将本次赋值作废
break;
}
}
}
blues[k] = (int) (Math.random() * 16 + 1);
}
}
break;
case 'n':
case 'N': {
for (int k = 0; k < count; k++) {
System.out.println("请输入第【" + (k + 1) + "】注双色球号码!");
System.out.println("请输入6个红色球号<数字:1-33>.");
System.out.print("第【1】个红色球号为:");
reds[k][0] = sc.nextInt();
while (reds[k][0] > 33 || reds[k][0] < 1) {
System.out.println("您输入的号数有误!范围应在1-33之间!\n");
System.out.print("请重新输入第【1】个红色球号:");
reds[k][0] = sc.nextInt();
}
for (int i = 1; i < reds[k].length; i++) {
//生成的开奖号码与前者不能相同
System.out.print("第【" + (i + 1) + "】个红色球号为:");
reds[k][i] = sc.nextInt();
if (reds[k][i] < 1 || reds[k][i] > 33) {
i--;
System.out.println("您输入的号数有误!号码范围为:1-33!");
continue;
}
for (int j = 0; j < i; j++) {
if (reds[k][i] == reds[k][j]) {
i--;//如果重复 i--相当于将本次赋值作废2
System.out.println("您输入的号数有误!不能与之前号数重复!");
break;
}
}
}
System.out.print("\n请输入1个蓝色球号<数字:1-16>:");
blues[k] = sc.nextInt();
while (blues[k] > 16 || blues[k] < 1) {
System.out.println("您输入的号数有误!范围应在1-16之间!\n");
System.out.print("请重新输入蓝色球号:");
blues[k] = sc.nextInt();
}
}
}
break;
}
System.out.println("您一共买了[" + count + "]注,共需支付[" + (count * price) + "]元!");
System.out.println("您所选的号码为:");
for (int i = 0; i < count; i++) {
System.out.print("第【" + (i + 1) + "】注双色球号码为:");
System.out.print("\t红色球:");
for (int j = 0; j < reds[i].length; j++) {
System.out.print(reds[i][j] + "\t");
}
System.out.println("蓝色球:" + blues[i]);
}
System.out.println();
}
//复式购买双色球(每注相同)
public static void buyDuplicate() {
type=0;
System.out.print("您需要下多少注?请输入:");
count = sc.nextInt();
System.out.println("是否让机器自动选择号数?请输入Y/N y/n");
char[] c = sc.next().toCharArray();
switch (c[0]) {
case 'y':
case 'Y': {
red[0] = (int) (Math.random() * 33 + 1);
for (int i = 1; i < red.length; i++) {
//生成的开奖号码与前者不能相同
red[i] = (int) (Math.random() * 33 + 1);
for (int j = 0; j < i; j++) {
if (red[i] == red[j]) {
i--;//如果重复 i--相当于将本次赋值作废
break;
}
}
}
blue = (int) (Math.random() * 16 + 1);
}
break;
case 'n':
case 'N': {
System.out.println("请输入6个红色球号<数字:1-33>.");
System.out.print("第【1】个红色球号为:");
red[0] = sc.nextInt();
while (red[0] > 33 || red[0] < 1) {
System.out.println("您输入的号数有误!范围应在1-33之间!\n");
System.out.print("请重新输入第【1】个红色球号:");
red[0] = sc.nextInt();
}
for (int i = 1; i < red.length; i++) {
//生成的开奖号码与前者不能相同
System.out.print("第【" + (i + 1) + "】个红色球号为:");
red[i] = sc.nextInt();
if (red[i] < 1 || red[i] > 33) {
i--;
System.out.println("您输入的号数有误!号码范围为:1-33!");
continue;
}
for (int j = 0; j < i; j++) {
if (red[i] == red[j]) {
i--;//如果重复 i--相当于将本次赋值作废
System.out.println("您输入的号数有误!不能与之前号数重复!");
break;
}
}
}
System.out.print("\n请输入1个蓝色球号<数字:1-16>:");
blue = sc.nextInt();
while (blue > 16 || blue < 1) {
System.out.println("您输入的号数有误!范围应在1-16之间!\n");
System.out.print("请重新输入蓝色球号:");
blue = sc.nextInt();
}
}
break;
}
System.out.println("您一共买了[" + count + "]注,共需支付[" + (count * price) + "]元!");
System.out.println("您所选的号码为:");
System.out.print("红色球:");
for (int i : red) {
System.out.print(i + "\t");
}
System.out.println("蓝色球:" + blue + "\n");
}
//查看中奖
public static void show() {
bool = false;
System.out.println("\n双色球彩票系统>>>>>>>>>查看中奖\n");
//中奖双色球号码随机生成
//随机生成红色球号码
redReal[0] = (int) (Math.random() * 33 + 1);
for (int i = 1; i < redReal.length; i++) {
//生成的开奖号码与前者不能相同
while (true) {
int index = 0;
redReal[i] = (int) (Math.random() * 33 + 1);
for (int j = 0; j < i; j++) {
if (redReal[i] == redReal[j]) {
index++;
}
}
if (index == 0) {
break;
}
}
}
blueReal = (int) (Math.random() * 16 + 1);
//显示开奖号码
System.out.println("本期开奖号码为:");
System.out.print("红色球:");
for (int i : redReal) {
System.out.print(i + "\t");
}
System.out.println("蓝色球:" + blueReal + "\n");
//判断彩票购买方式
if (type == 0) {
System.out.println("您已买的号码为:");
System.out.print("红色球:");
for (int i : red) {
System.out.print(i + "\t");
}
System.out.println("蓝色球:" + blue + "\n");
//显示中奖结果
getResult(getGrade(red, blue, redReal, blueReal));
} else {
System.out.println("您所选的号码为:");
for (int i = 0; i < count; i++) {
System.out.print("第【" + (i + 1) + "】注双色球号码为:");
System.out.print("\t红色球:");
for (int j = 0; j < reds[i].length; j++) {
System.out.print(reds[i][j] + "\t");
}
System.out.println("蓝色球:" + blues[i]);
}
System.out.println();
getResults(getGrades(reds, blues, redReal, blueReal));
}
}
//显示复式彩票中奖结果
public static void getResult(int grade) {
switch (grade) {
case 1:
System.out.println("恭喜您!一等奖!您下了【" + count + "】注,投资了【" + count * price + "】元,本次奖金为【" + (500 * count) + "】万元");
break;
case 2:
System.out.println("恭喜您!二等奖!您下了【" + count + "】注,投资了【" + count * price + "】元,本次奖金为【" + (400 * count) + "】万元");
break;
case 3:
System.out.println("恭喜您!三等奖!您下了【" + count + "】注,投资了【" + count * price + "】元,本次奖金为【" + (300 * count) + "】万元");
break;
case 4:
System.out.println("恭喜您!四等奖!您下了【" + count + "】注,投资了【" + count * price + "】元,本次奖金为【" + (200 * count) + "】万元");
break;
case 5:
System.out.println("恭喜您!五等奖!您下了【" + count + "】注,投资了【" + count * price + "】元,本次奖金为【" + (100 * count) + "】万元");
break;
case 6:
System.out.println("恭喜您!六等奖!您下了【" + count + "】注,投资了【" + count * price + "】元,本次奖金为【" + (5 * count) + "】元");
break;
default:
System.out.println("不好意思,未中奖,望再接再厉!您下了【" + count + "】注,投资了【" + count * price + "】元,本次奖金为【" + (0 * count) + "】元");
}
}
//显示单式彩票中奖结果
public static void getResults(int[] grades) {
double sum = 0;
for (int i = 0; i < count; i++) {
switch (grades[i]) {
case 1:
sum += 500;
System.out.println("第【" + (i + 1) + "】注中了一等奖!");
break;
case 2:
sum += 400;
System.out.println("第【" + (i + 1) + "】注中了二等奖!");
break;
case 3:
sum += 300;
System.out.println("第【" + (i + 1) + "】注中了三等奖!");
break;
case 4:
sum += 200;
System.out.println("第【" + (i + 1) + "】注中了四等奖!");
break;
case 5:
sum += 100;
System.out.println("第【" + (i + 1) + "】注中了五等奖!");
break;
case 6:
sum += 0.0005;
System.out.println("第【" + (i + 1) + "】注中了六等奖!");
break;
default:
System.out.println("第【" + (i + 1) + "】注未中奖!");
}
}
System.out.println("您下了【" + count + "】注,投资了【" + count * price + "】元,总奖金为【" + sum + "】万元");
}
//获取单式彩票获奖结果集合
public static int[] getGrades(int[][] reds, int[] blues, int[] redLuck, int blueLuck) {
int[] grades = new int[count];
for (int i = 0; i < count; i++) {
grades[i] = getGrade(reds[i], blues[i], redReal, blueReal);
}
return grades;
}
//获取获奖等级
public static int getGrade(int[] red, int blue, int[] redLuck, int blueLuck) {
int grade = 0;
int redCount = 0;
int blueCount = 0;
Arrays.sort(red);
Arrays.sort(redLuck);
//获取红色球相同个数
for (int i = 0; i < red.length; i++) {
for (int j = 0; j < redLuck.length; j++) {
if (red[i] == redLuck[j]) {
redCount++;
}
}
}
//获取蓝色球相同个数
if (blue == blueLuck) blueCount++;
if (redCount == 6 && blueCount == 1) {
grade = 1;
} else if (redCount == 6) {
grade = 2;
} else if ((redCount == 5) && (blueCount == 1)) {
grade = 3;
} else if ((redCount == 5) || (redCount == 4 && blueCount == 1)) {
grade = 4;
} else if ((redCount == 4) || (redCount == 3 && blueCount == 1)) {
grade = 5;
} else if (blueCount == 1) {
grade = 6;
}
return grade;
}
}