import java.util.Random;
import java.util.Scanner;
public class LotterySystem {
// 数据源
static int[][] lotteryArray = new int[100][7];
//存储账号信息 账号0列 密码1列
static String[][] usersArray = new String[100][2];
static Random random = new Random();
static Scanner scanner = new Scanner(System.in);
//金额
static int money = 100;
//记录是否登录
static boolean isLogin = false;
public static void main(String[] args) {
boolean isLoop = true;
System.out.println("欢迎进入坑你一局彩票系统!");
while (isLoop) {
System.out.println("1.注册");
System.out.println("2.登录");
System.out.println("3.充值");
System.out.println("4.查看余额");
System.out.println("5.购买彩票");
System.out.println("6.开奖");
System.out.println("7.查看彩票");
System.out.println("8.退出");
int choose = scanner.nextInt();
switch (choose) {
case 1:
register();
break;
case 2:
if (isLogin) {
System.out.println("已登录!可直接购买!");
break;
}
isLogin = loginMethod();
System.out.println(isLogin ? "登录成功!" : "登录失败!");
break;
case 5:
// 登录拦截
// if (!isLogin) {
// System.out.println("请先登录再购买!");
// break;
// }
buyLottery();
break;
case 6:
System.out.println("----");
// 开奖
int[] pArray = prizeLottery();
printArray(pArray);
//每注的中奖结果
int[][] countArray = prize();
//查看中了几等奖
printResult(countArray);
break;
case 8:
isLoop = false;
break;
default:
System.out.println("选错了把!重新选!");
break;
}
}
}
// 1.-----------------------------------
public static void register() {
String username;
while (true) {
System.out.println("请输入账号:");
username = scanner.next();
//账号不能重复注册!!
boolean isExits = false;//默认账号不存在
for (int i = 0; i < usersArray.length; i++) {
if (usersArray[i][0] != null && usersArray[i][0].equals(username)) {
System.out.println("账号已注册!");
isExits = true;
break;
}
}
if (isExits == false) {
break;
}
}
System.out.println("请输入密码:");
String password = scanner.next();
if (password != null) {
for (int i = 0; i < usersArray.length; i++) {
if (usersArray[i][0] == null) {
usersArray[i][0] = username;
usersArray[i][1] = password;
break;
}
}
System.out.println("注册成功!");
}
}
//5.--------------------
public static void buyLottery(){
System.out.println("输入要购买的注数:");
int count = scanner.nextInt();
//查看余额
if (isEnoughMoney(count) == false) {
System.out.println("余额不够,请充值!");
return;//直接结束代码!
}
//说明余额充足------
System.out.println("你说,机选还是自选!机选0,自选1");
int c = scanner.nextInt();
if (c == 0) {
//机选
//外层负责注数
for (int i = 0; i < count; i++) {
int [] oneLottery = new int[7];
for (int j = 0; j < 6; j++) {
int temp = random.nextInt(33) + 1;
// 查看temp是否重复
boolean isExit = isExits(oneLottery, temp);
//----
if (isExit) {
//存在
j--;
}else{
//可用
oneLottery[j] = temp;
}
}
//一维数组结束啦!!
maopao(oneLottery, 1);
//蓝球
oneLottery[6] = random.nextInt(16) + 1;
printArray(oneLottery);
//添加到数据源中
addLottery(oneLottery);
}
}
}
//查看余额是否足够能买
public static boolean isEnoughMoney(int count) {
if (money >= 2 * count) {
money -= 2 * count;
return true;
}
return false;
}
// ---------------------------
//封装冒泡排序--只排序前6个数
public static void maopao(int[] arr, int order) {
for (int i = 0; i < arr.length - 2; i++) {
for (int j = 0; j < arr.length - i - 2; j++) {
int temp = 0;
if(order == 1){
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}else if(order == 0){
if (arr[j] < arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
//-------------------------------------
//封装输出一维数组 (你可以再封装一个输出二维数组的)
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
// ------------------
//将一维数组 放入数据源二维数组中
public static void addLottery(int[] arr) {
for (int i = 0; i < lotteryArray.length; i++) {
if (lotteryArray[i][0] == 0) {
//这一行没有放彩票
for (int j = 0; j < arr.length; j++) {
lotteryArray[i][j] = arr[j];
}
break;//放完彩票 结束循环
}
}
}
//------------------------------
// 查看元素是否可用
public static boolean isExits(int[] arr, int num) {
boolean isExit = false;//默认不在
for (int k = 0; k < 6; k++) {
if(arr[k] == num){
isExit = true;
break;
}
}
return isExit;
}
// --------------------
public static boolean loginMethod() {
System.out.println("请输入账号");
String username = scanner.next();
System.out.println("请输入密码");
String passwd = scanner.next();
//遍历判断
for (int i = 0; i < usersArray.length; i++) {
if (usersArray[i][0] == null) {
break;
}
if(usersArray[i][0] != null && username.equals(usersArray[i][0])
&& usersArray[i][1].equals(passwd)){
return true;
}
}
return false;
}
// ----
//随机一注 作为开奖
public static int[] prizeLottery() {
int[] arr = new int[7];
for (int i = 0; i < arr.length - 1; i++) {
int temp = random.nextInt(33) + 1;
boolean isOK = isExits(arr, temp);
if (!isOK) {
arr[i] = temp;
}else {
i--;
}
}
//排序
maopao(arr, 1);
arr[6] = random.nextInt(16) + 1;
return arr;
}
// ---------------------------------
public static int[][] prize() {
//数组存储结果
int[][] countArray = new int[100][2];
//0 记录红球个数 1列记录蓝球个数
//1.中奖号码
int[] prizeArray = prizeLottery();
//2.遍历
for (int i = 0; i < lotteryArray.length; i++) {
if (lotteryArray[i][0] == 0) {
break;
}
for (int j = 0; j < 6; j++) {
for (int k = 0; k < 6; k++) {
if (prizeArray[k] == lotteryArray[i][j]) {
countArray[i][0]++;
}
}
}
// 红球比较结束
if(lotteryArray[i][6] == prizeArray[6]){
countArray[i][1] = 1;
}
}
return countArray;
}
//------------------------------------
//中了几等奖啊
public static void printResult(int[][] countArray) {
for (int i = 0; i < countArray.length; i++) {
if (lotteryArray[i][0] == 0) {
break;
}
switch (countArray[i][0]) {
case 6:
if (countArray[i][1] == 1) {
System.out.println("第" + (i+1) + "注中了一等价");
}else{
System.out.println("第" + (i+1) + "注中了二等价");
}
break;
case 5:
if (countArray[i][1] == 1) {
System.out.println("第" + (i+1) + "注中了三等价");
}else{
System.out.println("第" + (i+1) + "注中了四等价");
}
break;
case 4:
if (countArray[i][1] == 1) {
System.out.println("第" + (i+1) + "注中了四等价");
}else{
System.out.println("第" + (i+1) + "注中了五等价");
}
break;
case 3:
if (countArray[i][1] == 1) {
System.out.println("第" + (i+1) + "注中了五等价");
}else{
System.out.println("第" + (i+1) + "注中了阳光普照奖");
}
break;
case 2:
case 1:
if (countArray[i][1] == 1) {
System.out.println("第" + (i+1) + "注中了六等价");
}else{
System.out.println("第" + (i+1) + "注中了阳光普照奖");
}
break;
default:
break;
}
}
}
}