package cp.xql;import java.text.DecimalFormat; import java.util.Scanner;public class Demo {public static Integer[] cache; //缓存产生的彩票号码public static String[] lottynum; //把自选或机选产生的号码格式化时public static String[] pincNum; //开奖彩票号码public static int grade; //中奖的等级public static double money; //中奖的金额public static void main(String[] args) {System.out.println();System.out.println("--------------------------------");System.out.println("------------ 功能选择 ------------------");System.out.println("------------ 1.自选 2.机选 ------------------");System.out.println("------------ 3.开奖 4.兑奖 ------------------");System.out.println("------------ 0.退出 ------------------");while (true) {System.out.println("请开始你的选择");int key = wirte();switch (key) {case 1:useSelect();break;case 2:randomSelect();break;case 3:drawing();break;case 4:cash();break;case 0:System.out.println("拜拜了");System.exit(0);default:System.out.println("还有待开发");break;}}}private static void cash() {if (pincNum==null || lottynum==null){System.out.println("请购买彩票或没有开奖");return;}int readBall=0; int blueBall=0;for (int i = 0; i <pincNum.length-1 ; i++) {for (int j = 0; j <lottynum.length-1 ; j++) {if (pincNum[i].equals(lottynum[j])){//开奖的红球=购买彩票的红球readBall++;//供求数量加1}}}if (pincNum[6]==lottynum[6]){//如果开奖的篮球等于购买的篮球blueBall++;//供求数量加1}if(readBall==6 && blueBall==1){grade=1;}else if(readBall==6){grade=2;}else if (readBall==5 && blueBall==1){grade=3;}else if (readBall==5 ||(readBall==4 && blueBall==1)){grade=4;}else if(readBall==4 ||(readBall==3 && blueBall==1)){grade=5;}else if (blueBall==1 ||(readBall==1 && blueBall==1) ||(readBall==2 && blueBall==1) ){grade=6;}else {grade=0;}switch (grade){case 1:money=5000000;System.out.println("恭喜你 走狗屎运 一等奖"+money+"元");break;case 2:money=3000000;System.out.println("差一点 恭喜二等奖"+money+"元");break;case 3:money=200000;System.out.println("差一点 恭喜三等奖"+money+"元");case 4:money=20000;System.out.println(" 四等奖"+money+"元");case 5:money=10000;System.out.println(" 五等奖"+money+"元");case 6:money=5000;System.out.println(" 六等奖"+money+"元");default:System.out.println("太可惜");break;}} //开奖private static void drawing() {if (lottynum==null){System.out.println("请先购买彩票,想白嫖?");return;}cache =new Integer[7];randomNum();//这个时候产出的号码就是中奖号码bubbleSort();pincNum =new String[7];//格式化lotteryFormat(pincNum);System.out.println("--------------------");System.out.println("------------本期中奖号码是--------------------------");show(pincNum);System.out.println("-------------------------");System.out.println("---------您购买的彩票号码----------");show(lottynum);}private static void randomSelect() {System.out.println("正在出号中...");cache = new Integer[7];randomNum();bubbleSort();lottynum = new String[7];lotteryFormat(lottynum);show(lottynum);}/*** 机选的方法*/public static void randomNum() {for (int i = 0; i < 6; i++) {cache[i] = Math.toIntExact(Math.round(Math.random() * 32 + 1));if (!checkReBall(i)) {i--;}}for (int i = 6; i < 7; i++) {cache[i] = Math.toIntExact(Math.round(Math.random() * 15 + 1));if (!checkBlueBall(i)) {i--;}}}/*** 自选的方法*/private static void useSelect() {System.out.println("--------------");System.out.println("----------开始自选号码,一共七位");System.out.println("前六位 1-33");System.out.println("后一位蓝色号码 1-16");cache = new Integer[7];for (int i = 0; i < 6; i++) {System.out.println("请输入" + (i + 1) + "红球");cache[i] = wirte();if (!checkReBall(i)) {i--;}}for (int i = 6; i < 7; i++) {System.out.println("请输入" + (i + 1) + "蓝球");cache[i] = wirte();if (!checkBlueBall(i)) {i--;}}bubbleSort();//冒泡排序lottynum = new String[7];//范围lotteryFormat(lottynum);//保存着格式化之后的数据show(lottynum);//展示}/*** 判断蓝球 和 红球的范围*/public static boolean checkReBall(int index) {if (cache[index] < 1 || cache[index] > 33) {System.out.println("输入超过红球的范围了");return false;}for (int i = 0; i < index; i++) {if (cache[i] == cache[index]) {System.out.println("红球不能重复");return false;}}return true;}public static boolean checkBlueBall(int index) {if (cache[index] < 1 || cache[index] > 16) {System.out.println("输入超过蓝球的范围了");return false;}return true;}/*** 判断是否输出正确*/public static int wirte() {int key;try {key = new Scanner(System.in).nextInt();} catch (Exception e) {System.out.println("你输入错误,请重新输入");key = wirte();}return key;}/*** 冒泡排序*/public static void bubbleSort() {for (int i = 0; i < cache.length - 2; i++) {for (int j = 0; j < cache.length - 2 - i; j++) {if (cache[j] > cache[j + 1]) {Integer temp = cache[j + 1];cache[j + 1] = cache[j];cache[j] = temp;}}}}/*** 格式化数据*/private static void lotteryFormat(String[] array) {DecimalFormat df = new DecimalFormat("00");for (int i = 0; i < cache.length; i++) {array[i] = df.format(cache[i]);}}/*** 输出*/private static void show(String[] array) {for (int i = 0; i < 6; i++) {System.out.print("红" + (i + 1) + "\t");}System.out.print("蓝" + "\n");for (String o : array) {System.out.print((String) o + "\t");}} }