模拟双色球

模拟双色球

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.Random;
import java.util.Scanner;

public class Main {
static Random random = new Random();
static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
int[] winCode = new int[7];
int[] userCode = new int[7];
int countBlue = 0, countRed = 0;
winCode(winCode);
System.out.println("中奖号码为:");
for (int i = 0; i < winCode.length; i++) {
System.out.print(winCode[i] + " ");
}
System.out.println();
for (int i = 0; i < userCode.length - 1; i++) {
System.out.println("请输入第" + (i + 1) + "位红球");
int num = scanner.nextInt();
while (num > 33 || num < 1) {
System.out.println("请输入1-33之间的数");
num = scanner.nextInt();
}
userCode[i] = num;
}
System.out.println("请输入蓝球");
int num = scanner.nextInt();
while (num > 16 || num < 1) {
System.out.println("请输入1-16之间的数");
num = scanner.nextInt();
}
userCode[6] = num;
// 判断中奖情况
for (int i = 0; i < winCode.length - 1; i++) {
for (int j = 0; j < userCode.length - 1; j++) {
if (winCode[j] == userCode[i]) {
countRed++;
}
}
}
if (winCode[6] == userCode[6]) {
countBlue++;
}
System.out.println("你的号码为:");
for (int i = 0; i < userCode.length; i++) {
System.out.print(userCode[i] + " ");
}
System.out.println();
if ((countRed == 0 && countBlue == 1) || (countRed == 1 && countBlue == 1) || (countRed == 2 && countBlue == 1)) {
System.out.println("感谢您中了六等奖");
} else if ((countRed == 3 && countBlue == 0) || (countRed == 4 && countBlue == 0)) {
System.out.println("恭喜您中了五等奖");
} else if ((countRed == 4 && countBlue == 1) || (countRed == 5 && countBlue == 0)) {
System.out.println("恭喜您中了四等奖");
} else if (countRed == 5 && countBlue == 1) {
System.out.println("恭喜您中了三等奖");
} else if ((countRed == 6 && countBlue == 0)) {
System.out.println("恭喜您中了二等奖");
} else if (countRed == 6 && countBlue == 1) {
System.out.println("恭喜您中了一等奖");
} else {
System.out.println("很遗憾,您没有中奖");
}

}

public static void winCode(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
a[i] = random.nextInt(33) + 1;
for (int j = 0; j < i; j++) {
if (a[i] == a[j]) {
i--;
break;
}
}
}
a[a.length - 1] = random.nextInt(16) + 1;
}
}