能够实现的功能有六个:(1)充值 (2)查看账户资金(3)开始购买彩票(4)开奖 (5)查看当前购买的彩票(6)退出系统
import java.util.Arrays; import java.util.Random; import java.util.Scanner;
public class Shuangseqiu { public static void main(String[] args) { // TODO
Auto-generated method stub /*1、提醒“欢迎进入***抽奖系统” 双色球 */
System.out.println("欢迎进入双色球抽奖系统"); //command是记录命令的字符,只有1—6可以选择系统中已经存在的命令 String
command = null; //accountMoney是账户余额,MoneyEarned是彩票中的钱 int accountMoney =
20,MoneyEarned = 0; Scanner sc = new Scanner(System.in); // int[][] UserLotter
= new int[10][8]; int[][] UserNumber = new int[10][7]; int[] SystemNumber = new
int[7]; Random ran = new Random(); int LotterNum = 0;//用户选择彩票的注数 while (true) {
// 请选择 // 1:充值 2:查看账户资金 3:开始购买彩票 4:开奖 5:查看当前购买的彩票 6.退出系统
System.out.println("请输入您的命令,1:充值 2:查看账户资金 3:开始购买彩票 4:开奖 5:查看当前购买的彩票 6.退出系统");
// LotterNum = 0; command = sc.nextLine(); switch (command) { case "1": // 1、充值
用户自己录入充值金额,显示充值之后账户资金 System.out.println("请输入一个为整数的充值金额:"); int Money =
sc.nextInt(); System.out.println("原先账户金额为"+accountMoney+"元"); accountMoney +=
Money; System.out.println("充值之后,账户金额为"+accountMoney+"元"); break; case "2": //
2:查看账户资金 System.out.println("账户金额为"+accountMoney+"元"); break; case "3":
/*3、开始购买彩票 购买彩票注数 10 【共7位,前6位1-32,第七位是1-16】
一注【1,2,5,8,20,32,16】不能重复且前6位从小到大有序 验证当前账户资金是否充足?一注2块 1)自选彩票 自己录入7个数值 2)机选
随机生成彩票 随机给用户出彩票,用户资金减少*/ System.out.println("输入您想选择的注数,最多可以选择10注"); String
Number = sc.nextLine(); LotterNum = Integer.parseInt(Number);
//用来存储用户输入到系统中的红球和蓝球 // int[][] UserNumber = new int[LotterNum][7];
if(accountMoney<(LotterNum*2)) { System.out.println("余额不足,请您去充值"); break; }else
{ accountMoney-=LotterNum*2;
System.out.println("开始选择彩票,输入8开始自选彩票,输入其他数字,就是机选彩票"); String chooseCommand =
sc.nextLine(); if(chooseCommand.equals("8")) { System.out.println("开始自己选择彩票");
for(int i = 0;i < LotterNum;i++) { for(int j = 0;j < UserNumber[i].length;j++)
{ if(j<6){ System.out.print("请输入第"+(i+1)+"注中的第"+(j+1)+"个红球:"); String num =
sc.nextLine(); UserNumber[i][j] = Integer.parseInt(num);
if(UserNumber[i][j]<=32&&UserNumber[i][j]>=1) { for(int k = 0;k < j;k++) {
if(UserNumber[i][k] == UserNumber[i][j]) { j--;
System.out.println("与前面输入的数重复了,请重新输入"); break; } } }else{ j--;
System.out.println("输入的数必须大于等于1小于等于32,您输的数据不符合要求,请重新输入"); } }else{
System.out.println("\n请输入第"+(i+1)+"注中的蓝球:"); String num = sc.nextLine();
UserNumber[i][j] = Integer.parseInt(num);
if(UserNumber[i][j]<1||UserNumber[i][j]>16) {
System.out.println("您输入的数字不在1到16之间,请重新输入"); j--; } } } } }else {
System.out.println("开始机器选择彩票"); for(int i = 0;i < LotterNum;i++) { for(int j =
0;j < UserNumber[i].length;j++) { if(j<6){ int num = ran.nextInt(32)+1;
UserNumber[i][j] = num; for(int k = 0;k < j;k++) { if(UserNumber[i][k] ==
UserNumber[i][j]) { j--; break; } } }else{ int num = ran.nextInt(16)+1;
UserNumber[i][6] = num; } } } } } int temp; for (int t = 0; t < LotterNum; t++)
{ for (int m = 0; m < 5; m++) { for (int n = 0; n < 5-m; n++) { if
(UserNumber[t][n] > UserNumber[t][n+1]) { temp = UserNumber[t][n];
UserNumber[t][n] = UserNumber[t][n+1]; UserNumber[t][n+1] = temp; } } } }
for(int i = 0;i < LotterNum;i++) { System.out.print("红球为"); for(int j = 0;j <
UserNumber[i].length;j++) { if(j<6) { System.out.print(UserNumber[i][j]+" ");
}else { System.out.print("蓝球为"+UserNumber[i][j]); } } System.out.println(); }
break; case "4": /*4、开奖 随机产生一注彩票,然后和用户的彩票数组比对, 如果红色球有三个相同/或者一个蓝球,中5元
如果红色球3个+1个蓝色球,中10元 如果红色球4个,中20 如果四个红色球+1个蓝色球,中200 如果5个红色球,中1000
如果5个红色球+1个蓝色球,中2万 如果6个红色球,中红10万 如果6个红色球+1个蓝色球,中500万 账户余额更新*/ for(int
i=0;i<6;i++) { SystemNumber[i] = ran.nextInt(32)+1; for(int k = 0;k < i;k++) {
if(SystemNumber[i] == SystemNumber[k]) { i--; break; } } } SystemNumber[6] =
ran.nextInt(16)+1; for (int m = 0; m < 5; m++) { for (int n = 0; n < 5-m; n++)
{ if (SystemNumber[n] > SystemNumber[n+1]) { temp = SystemNumber[n];
SystemNumber[n] = SystemNumber[n+1]; SystemNumber[n+1] = temp; } } } for(int t
= 0;t<LotterNum;t++) { int moneyTemp = 0; int numberWin = 0; for (int n = 0; n
< 6; n++) { for(int m = 0;m<6;m++) { if(SystemNumber[m]==UserNumber[t][n]) {
numberWin++; } } } if(SystemNumber[6]==UserNumber[t][6]) { switch(numberWin) {
case 0: case 1: case 2: moneyTemp = 5; break; case 3: moneyTemp = 10; break;
case 4: moneyTemp = 200; break; case 5: moneyTemp = 20000; break; case 6:
moneyTemp = 5000000; break; } }else { switch(numberWin) { case 0: case 1: case
2: moneyTemp = 0; break; case 3: moneyTemp = 5; break; case 4: moneyTemp = 20;
break; case 5: moneyTemp = 1000; break; case 6: moneyTemp = 10000; break; } }
if(SystemNumber[6]==UserNumber[t][6]) {
System.out.println("第"+(t+1)+"注中了"+numberWin+"个红球,蓝球也中了,中了"+moneyTemp+"元");
}else {
System.out.println("第"+(t+1)+"注中了"+numberWin+"个红球,蓝球没中了,中了"+moneyTemp+"元"); }
MoneyEarned += moneyTemp; }
System.out.println("开奖号码为"+Arrays.toString(SystemNumber));
System.out.println("您所买的"+LotterNum+"注,共中了"+MoneyEarned+"元");
accountMoney+=MoneyEarned; System.out.println("账户余额为"+accountMoney); LotterNum
= 0; MoneyEarned = 0; break; case "5": // 5:查看当前购买的彩票 if(UserNumber[0][0]==0) {
System.out.println("您当前没有购买彩票"); }else { for(int i = 0;i < LotterNum;i++) {
System.out.print("红球为"); for(int j = 0;j < UserNumber[i].length;j++) { if(j<6)
{ System.out.print(UserNumber[i][j]+" "); }else {
System.out.print("蓝球为"+UserNumber[i][j]); } } System.out.println(); } } break;
case "6": // 6.退出系统 System.exit(1); break; default:
System.out.println("输入指令有误,请重新购买"); } } } }