双色球当今时代很是流行,笔者觉得这种游戏比较简单适合于我这样初级水平的人研究,所以闲暇之余将自己思路展现给大家,不能说是分享,因为并非佳品。目的很单纯,就是扔一块硬臭砖头,希望能有幸看到玉石的纯洁光芒。对于其中的不足,欢迎读者的批评和赐教。
双色球的投注规则有很多,这里只研究单注的情形。
双色球单注下注规则如下:
双色球投注区分为红球号码区和蓝球号码区,红球号码范围为01~33,蓝球号码范围为01~16。双色球每期从33个红球中开出6个号码,从16个蓝球中开出1个号码作为中奖号码,双色球玩法即是竞猜开奖号码的6个红球号码和1个蓝球号码,顺序不限。
程序需求:
模拟1.随机产生序列,2.手工输入序列,3.能够退出,在退出前重复1和2
废话不多说直接上代码:
1 package com.bingbing.blogs;
2
3 import java.util.Scanner;
4
5 /**
6 * 双色球生成规则:红球 1-33 蓝球1-16 不放回抽取出6个红球 +1个篮球完成以下功能: 1.机选一次 2.手动选择 3.退出要求:
7 * 如果机选或手动输入的都要求输出的是有序的 7 16 21 22 31 33 + 11
8 * */
9 public class DoubleBall {
10
11 private static Scanner sc = new Scanner(System.in);
12 private static int[] array = new int[7];
13 private static boolean judge = true;
14
15 public static void main(String[] args) {
16
17 while (judge) {
18 System.out.println("***************************************");
19 System.out.println("请选择操作:\n1.机选一次\n2.手动选择\n3.退出");
20 System.out.println("***************************************");
21
22 int choice = sc.nextInt();
23
24 switch (choice) {
25 case 1:
26 machine();
27 show();
28 break;
29 case 2:
30 hand();
31 show();
32 break;
33 case 3:
34 quit();
35 break;
36 default:
37 System.out.println("您输入的选项不正确,请检查");
38 break;
39 }
40 }
41
42 }
43
44 // 打印出双色球的编号
45
46 private static void show() {
47
48 System.out.print("红球: ");
49 for (int i = 0; i < array.length - 1; i++) {
50 System.out.print(array[i] + " ");
51 }
52 System.out.print("\n蓝球:" + array[array.length - 1] + "\n");
53 }
54
55 // 退出程序
56
57 private static void quit() {
58
59 judge = false;
60 System.out.println("已退出");
61
62 }
63
64 // 手动选号,这种方法适合狗屎运旺盛的家伙
65
66 private static void hand() {
67 boolean ju;
68 do {
69 System.out
70 .println("请输入6个红球的编号(1-33)和1个蓝球的编号(1-16)\n请使用空格将输入的每个数字隔开");
71 for (int i = 0; i < array.length; i++) {
72 array[i] = sc.nextInt();
73 }
74 bubleSort(array);
75 if (!(ju = isArrayCorrect(array))) {
76 System.out.println("您输入的数字不合法");
77 }
78 } while (!ju);
79
80 }
81
82 // 机器选号,省时省力,听天由命
83
84 private static void machine() {
85
86 do {
87 for (int i = 0; i < array.length - 1; i++) {
88 array[i] = (int) (Math.random() * 33 + 1);
89 }
90 array[array.length - 1] = (int) (Math.random() * 16 + 1);
91 bubleSort(array);
92 } while (!isArrayCorrect(array));
93
94 System.out.println("您得到了以下随机编号:");
95
96 }
97
98 // 传说中的冒泡排序,有木有高大上的赶脚
99
100 // 要有这一步是因为彩票的编号习惯是从左到右数值依次增大
101
102 private static void bubleSort(int[] a) {
103
104 int temp = a[0];
105 for (int i = a.length - 3; i >= 0; i--) {
106 for (int j = 0; j <= i; j++) {
107 if (a[j] > a[j + 1]) {
108 temp = a[j + 1];
109 a[j + 1] = a[j];
110 a[j] = temp;
111 }
112 }
113 }
114 }
115
116 // 判断给定数组是否满足数字编号不重复、范围合法等要求
117
118 private static boolean isArrayCorrect(int[] a) {
119
120 for (int i = 0; i < a.length - 1; i++) {
121 for (int j = 0; j < i; j++) {
122 if (array[j] == array[j + 1] || a[j] > 33 || a[j] < 1
123 || a[a.length - 1] < 1 || a[a.length - 1] > 16) {
124 return false;
125 }
126 }
127 }
128 return true;
129 }
130
131 }