双色球控制台版
技术需要
- 双重数组
- for、while循环
- if switch 分支判断
- break continue 循环控制
时间不够再打一边电子档,
如果大家有问题可以私聊或者留言问我,
电子版有点乱,(。・_・。)ノI’m sorry~
整体代码在最后,
好记性不如烂笔头,
希望大家还是自己整理一遍思路后再敲一遍
不要懒欧(*^▽^*)
变量创建
Scanner sc = new Scanner(System.in);int menu = 0;int[][] buy = new int[200][8];int buy_RedCount = 0;int buy_RedTemp = 0;int buy_BlueTemp = 0;int buy_MultipleTemp = 0;int buy_Count = 0;char buy_Continue = 'n';boolean redSort_Flag = false;boolean open_Flag = false;Random open_Random = new Random();int[] open = new int[7];int open_RedTemp = 0;int open_RedCount = 0;int view_RedCount = 0;int view_BlueCount = 0;int[] view = {0, 0, 0, 0};int view_Multiple = 0;int view_Sum = 0;
欢迎界面
//WelcomeSystem.out.println("\n****** ******* ******* ******* ******* ******* *******");System.out.println("Welcome to Two-Color ball!欢迎━(*`∀´*)ノ亻!");System.out.println("****** ******* ******* ******* ******* ******* *******");
菜单及do-while
o1:do {System.out.println("Please select the function by numbers(*^▽^*)");System.out.println("1. Buy lottery;");System.out.println("2. View purchased numbers;");System.out.println("3. Open the price;;");System.out.println("4. View the Winners;");System.out.println("5. Exit;");menu = sc.nextInt();switch (menu) {}} while (true);
case1
case 1:if (open_Flag){System.out.println("\nJust able to buy lotteries before open price!!!\n");break ;}System.out.println("\nBuy lottery:");while (true) {buy_RedCount = 0;//red-ballo2:while (true) {//input red-ballsSystem.out.println("Please input No." + (buy_Count + 1) + " lottery's red-ball's No." + (buy_RedCount + 1) + " number");buy_RedTemp = sc.nextInt();if (buy_RedTemp < 1 || buy_RedTemp > 31) {System.out.println("\nPlease input the number between 1 and 31 again!!!\n");continue;} else {for (int i = 0; i < buy_RedCount; i++) {if (buy_RedTemp == buy[buy_Count][i]) {System.out.println("\nCan't select the same ball!!!");System.out.println("Please input the number again!!!\n");continue o2;}}buy[buy_Count][buy_RedCount] = buy_RedTemp;buy_RedCount++;if (buy_RedCount == 6) {for (int i = 0; i < buy_RedCount - 1; i++) {for (int i1 = 0; i1 < buy_RedCount - 1 - i; i1++) {if (buy[buy_Count][i1] > buy[buy_Count][i1 + 1]) {buy_RedTemp = buy[buy_Count][i1];buy[buy_Count][i1] = buy[buy_Count][i1 + 1];buy[buy_Count][i1 + 1] = buy_RedTemp;redSort_Flag = true;}}if (!redSort_Flag) {break;}}break o2;}}}//blue-ballwhile (true) {System.out.println("Please input the blue-ball number:");buy_BlueTemp = sc.nextInt();if (buy_BlueTemp < 1 || buy_BlueTemp > 16) {System.out.println("\nPlease input the number again!!!\n");continue;} else {buy[buy_Count][buy_RedCount] = buy_BlueTemp;break;}}//multiplewhile (true) {System.out.println("Please input the multiple:");buy_MultipleTemp = sc.nextInt();if (buy_MultipleTemp < 2) {System.out.println("\nPlease input the number more than 1!!!\n");continue;} else {buy[buy_Count][buy_RedCount + 1] = buy_MultipleTemp;break;}}buy_Count++;//continueSystem.out.println("\nWhether what to buy a lottery again?(y/n)");buy_Continue = sc.next().charAt(0);if (buy_Continue == 'y' || buy_Continue == 'Y') {System.out.println();continue;} else {System.out.println("\nAbout exit purchase system...");System.out.println("Good luck!\n");break;}}break;
case2
case 2:System.out.println("\nView purchased numbers:");for (int i = 0; i < buy_Count; i++) {System.out.println("--------------------Lottery_" + (i + 1) + "------------------");System.out.print("Red-Ball Numbers : ");for (int j = 0; j < buy_RedCount; j++) {System.out.print(buy[i][j] + "\t");}System.out.println();System.out.print("Blue-Ball Numbers : ");System.out.println(buy[i][buy_RedCount]);System.out.print("Multiple Numbers : ");System.out.println(buy[i][buy_RedCount + 1]);}System.out.println();break;
case3
case 3://just able to open onceif(open_Flag){System.out.println("\nJust able to open the price once!!!\n");break ;}System.out.println("\nOpen the price:");//set flagopen_Flag = true;//generalize the red-ballo3:while(true) {//use open_RedTemp rather than directly use arrayopen_RedTemp = open_Random.nextInt(31) + 1;//remove the same randomfor (int i = 0; i < open_RedCount; i++) {if (open_RedTemp == open[i]) {continue o3;}}open[open_RedCount] = open_RedTemp;open_RedCount++;//exit red-ball generalizeif(open_RedCount==6){break ;}}//sort red-ballfor (int i = 0; i < 6 - 1; i++) {for (int i1 = 0; i1 < 6 - 1 - i; i1++) {if (open[i1]>open[i1+1]){open_RedTemp = open[i1];open[i1] = open[i1 + 1];open[i1 + 1] = open_RedTemp;}}}//generalize blue-ballopen[6] = open_Random.nextInt(16) + 1;System.out.println("The winning red-ball numbers:");for (int i = 0; i < 6; i++) {System.out.print(open[i]+"\t");}System.out.println();System.out.println("The winning blue-ball numbers:");System.out.print(open[6]);System.out.println("\n");break;
case4
case 4:if (buy_Count==0){System.out.println("\nPlease buy lotteries firstly!!!\n");break ;}if (!open_Flag){System.out.println("\nPlease open price firstly!!!\n");break ;}System.out.println("\nView the Winners:");for (int i = 0; i < buy_Count; i++) {view_RedCount = 0;view_BlueCount = 0;//output the buy lotteriesSystem.out.println("--------------The No."+(i+1)+" lottery---------------");System.out.print("The red-ball numbers:");for (int i1 = 0; i1 < 6; i1++) {System.out.print(buy[i][i1]+"\t");}System.out.println();System.out.print("The blue-ball numbers:" + buy[i][6]);System.out.println();//judge the red-ball number and sumSystem.out.print("The red-ball winning numbers:");for (int i1 = 0; i1 < open.length-1; i1++) {for (int i2 = 0; i2 < 6; i2++) {if(open[i1] == buy[i][i2]){System.out.print(open[i1]+"\t");view_RedCount++;}}}System.out.println();//judge the blue-ballSystem.out.print("The blue-ball winning numbers:");if(open[6]==buy[i][6]){System.out.print(open[6]);view_BlueCount++;}System.out.println();//judge the level of priceif(view_RedCount==6&&view_BlueCount==1){System.out.println("Get the first price!!! The price is "+5000000*buy[i][7]);view_Sum += 5000000 * buy[i][7];view[0]++;} else if (view_RedCount==6&&view_BlueCount==0) {System.out.println("Get the second price!!! The price is "+500000*buy[i][7]);view_Sum += 500000 * buy[i][7];view[1]++;}else if (view_RedCount==5&&view_BlueCount==1) {System.out.println("Get the third price!!! The price is "+50000*buy[i][7]);view_Sum += 50000 * buy[i][7];view[2]++;}else if (view_RedCount==5&&view_BlueCount==0 || view_RedCount==4&&view_BlueCount==1) {System.out.println("Get the forth price!!! The price is "+200*buy[i][7]);view_Sum += 200 * buy[i][7];view[3]++;}}System.out.println("------------------------In the end--------------------------");if (view[0]!=0) {System.out.println("In the end , you have win the first price " + view[0] + " times");}if (view[1]!=0) {System.out.println("In the end , you have win the second price "+view[1]+" times");}if (view[2]!=0) {System.out.println("In the end , you have win the third price "+view[2]+" times");}if (view[3]!=0) {System.out.println("In the end , you have win the forth price "+view[3]+" times");}System.out.println("Totally get the bonus " + view_Sum);System.out.println();break;
case5&&default
case 5:System.out.println("\nExit:");System.out.println("About to exit system......");System.out.println("ヾ(ToT)Bye~Bye~");break o1;default:System.out.println("\nPlease input a correct again!!![○・`Д´・ ○]\n");break;
整体代码
public static void main(String[] args) {Scanner sc = new Scanner(System.in);int menu = 0;int[][] buy = new int[200][8];int buy_RedCount = 0;int buy_RedTemp = 0;int buy_BlueTemp = 0;int buy_MultipleTemp = 0;int buy_Count = 0;char buy_Continue = 'n';boolean redSort_Flag = false;boolean open_Flag = false;Random open_Random = new Random();int[] open = new int[7];int open_RedTemp = 0;int open_RedCount = 0;int view_RedCount = 0;int view_BlueCount = 0;int[] view = {0, 0, 0, 0};int view_Multiple = 0;int view_Sum = 0;//WelcomeSystem.out.println("\n****** ******* ******* ******* ******* ******* *******");System.out.println("Welcome to Two-Color ball!欢迎━(*`∀´*)ノ亻!");System.out.println("****** ******* ******* ******* ******* ******* *******");o1:do {System.out.println("Please select the function by numbers(*^▽^*)");System.out.println("1. Buy lottery;");System.out.println("2. View purchased numbers;");System.out.println("3. Open the price;;");System.out.println("4. View the Winners;");System.out.println("5. Exit;");menu = sc.nextInt();switch (menu) {case 1:if (open_Flag){System.out.println("\nJust able to buy lotteries before open price!!!\n");break ;}System.out.println("\nBuy lottery:");while (true) {buy_RedCount = 0;//red-ballo2:while (true) {//input red-ballsSystem.out.println("Please input No." + (buy_Count + 1) + " lottery's red-ball's No." + (buy_RedCount + 1) + " number");buy_RedTemp = sc.nextInt();if (buy_RedTemp < 1 || buy_RedTemp > 31) {System.out.println("\nPlease input the number between 1 and 31 again!!!\n");continue;} else {for (int i = 0; i < buy_RedCount; i++) {if (buy_RedTemp == buy[buy_Count][i]) {System.out.println("\nCan't select the same ball!!!");System.out.println("Please input the number again!!!\n");continue o2;}}buy[buy_Count][buy_RedCount] = buy_RedTemp;buy_RedCount++;if (buy_RedCount == 6) {for (int i = 0; i < buy_RedCount - 1; i++) {for (int i1 = 0; i1 < buy_RedCount - 1 - i; i1++) {if (buy[buy_Count][i1] > buy[buy_Count][i1 + 1]) {buy_RedTemp = buy[buy_Count][i1];buy[buy_Count][i1] = buy[buy_Count][i1 + 1];buy[buy_Count][i1 + 1] = buy_RedTemp;redSort_Flag = true;}}if (!redSort_Flag) {break;}}break o2;}}}//blue-ballwhile (true) {System.out.println("Please input the blue-ball number:");buy_BlueTemp = sc.nextInt();if (buy_BlueTemp < 1 || buy_BlueTemp > 16) {System.out.println("\nPlease input the number again!!!\n");continue;} else {buy[buy_Count][buy_RedCount] = buy_BlueTemp;break;}}//multiplewhile (true) {System.out.println("Please input the multiple:");buy_MultipleTemp = sc.nextInt();if (buy_MultipleTemp < 2) {System.out.println("\nPlease input the number more than 1!!!\n");continue;} else {buy[buy_Count][buy_RedCount + 1] = buy_MultipleTemp;break;}}buy_Count++;//continueSystem.out.println("\nWhether what to buy a lottery again?(y/n)");buy_Continue = sc.next().charAt(0);if (buy_Continue == 'y' || buy_Continue == 'Y') {System.out.println();continue;} else {System.out.println("\nAbout exit purchase system...");System.out.println("Good luck!\n");break;}}break;case 2:System.out.println("\nView purchased numbers:");for (int i = 0; i < buy_Count; i++) {System.out.println("--------------------Lottery_" + (i + 1) + "------------------");System.out.print("Red-Ball Numbers : ");for (int j = 0; j < buy_RedCount; j++) {System.out.print(buy[i][j] + "\t");}System.out.println();System.out.print("Blue-Ball Numbers : ");System.out.println(buy[i][buy_RedCount]);System.out.print("Multiple Numbers : ");System.out.println(buy[i][buy_RedCount + 1]);}System.out.println();break;case 3://just able to open onceif(open_Flag){System.out.println("\nJust able to open the price once!!!\n");break ;}System.out.println("\nOpen the price:");//set flagopen_Flag = true;//generalize the red-ballo3:while(true) {//use open_RedTemp rather than directly use arrayopen_RedTemp = open_Random.nextInt(31) + 1;//remove the same randomfor (int i = 0; i < open_RedCount; i++) {if (open_RedTemp == open[i]) {continue o3;}}open[open_RedCount] = open_RedTemp;open_RedCount++;//exit red-ball generalizeif(open_RedCount==6){break ;}}//sort red-ballfor (int i = 0; i < 6 - 1; i++) {for (int i1 = 0; i1 < 6 - 1 - i; i1++) {if (open[i1]>open[i1+1]){open_RedTemp = open[i1];open[i1] = open[i1 + 1];open[i1 + 1] = open_RedTemp;}}}//generalize blue-ballopen[6] = open_Random.nextInt(16) + 1;System.out.println("The winning red-ball numbers:");for (int i = 0; i < 6; i++) {System.out.print(open[i]+"\t");}System.out.println();System.out.println("The winning blue-ball numbers:");System.out.print(open[6]);System.out.println("\n");break;case 4:if (buy_Count==0){System.out.println("\nPlease buy lotteries firstly!!!\n");break ;}if (!open_Flag){System.out.println("\nPlease open price firstly!!!\n");break ;}System.out.println("\nView the Winners:");for (int i = 0; i < buy_Count; i++) {view_RedCount = 0;view_BlueCount = 0;//output the buy lotteriesSystem.out.println("--------------The No."+(i+1)+" lottery---------------");System.out.print("The red-ball numbers:");for (int i1 = 0; i1 < 6; i1++) {System.out.print(buy[i][i1]+"\t");}System.out.println();System.out.print("The blue-ball numbers:" + buy[i][6]);System.out.println();//judge the red-ball number and sumSystem.out.print("The red-ball winning numbers:");for (int i1 = 0; i1 < open.length-1; i1++) {for (int i2 = 0; i2 < 6; i2++) {if(open[i1] == buy[i][i2]){System.out.print(open[i1]+"\t");view_RedCount++;}}}System.out.println();//judge the blue-ballSystem.out.print("The blue-ball winning numbers:");if(open[6]==buy[i][6]){System.out.print(open[6]);view_BlueCount++;}System.out.println();//judge the level of priceif(view_RedCount==6&&view_BlueCount==1){System.out.println("Get the first price!!! The price is "+5000000*buy[i][7]);view_Sum += 5000000 * buy[i][7];view[0]++;} else if (view_RedCount==6&&view_BlueCount==0) {System.out.println("Get the second price!!! The price is "+500000*buy[i][7]);view_Sum += 500000 * buy[i][7];view[1]++;}else if (view_RedCount==5&&view_BlueCount==1) {System.out.println("Get the third price!!! The price is "+50000*buy[i][7]);view_Sum += 50000 * buy[i][7];view[2]++;}else if (view_RedCount==5&&view_BlueCount==0 || view_RedCount==4&&view_BlueCount==1) {System.out.println("Get the forth price!!! The price is "+200*buy[i][7]);view_Sum += 200 * buy[i][7];view[3]++;}}System.out.println("------------------------In the end--------------------------");if (view[0]!=0) {System.out.println("In the end , you have win the first price " + view[0] + " times");}if (view[1]!=0) {System.out.println("In the end , you have win the second price "+view[1]+" times");}if (view[2]!=0) {System.out.println("In the end , you have win the third price "+view[2]+" times");}if (view[3]!=0) {System.out.println("In the end , you have win the forth price "+view[3]+" times");}System.out.println("Totally get the bonus " + view_Sum);System.out.println();break;case 5:System.out.println("\nExit:");System.out.println("About to exit system......");System.out.println("ヾ(ToT)Bye~Bye~");break o1;default:System.out.println("\nPlease input a correct again!!![○・`Д´・ ○]\n");break;}} while (true);}