模拟双色球**开奖结果:随机生成一注双色球**,判断其是否中奖,奖金为多少。
程序员文章站
2024-02-02 15:21:40
...
- 一注双色球**由6个不重复的红球(序号:1~32)和1个篮球(序号:1~16)构成。
双色球中奖规则如下:
先进行摇号 然后产生一个中奖号码 然后判断是否中奖
摇号没有顺序之分 加入中奖号码为 1234567 摇号为6543217 也为1等奖
excel.java
import java.util.Random;
/**
* @author bcahlit
*
*/
public class excel {
int[] red;
int blue;
String name;
/**
* @param name
* 生成**的所有者 或者 开奖号码,用于在开奖是进行区分
*/
public excel(String name) {
this.name = name;
Random R = new Random();
this.red = new int[6];
this.blue = R.nextInt(15) + 1;
for (int i = 0; i < red.length; i++) {
int temR = R.nextInt(31) + 1;
while (existe(this.red, temR)) {
temR = R.nextInt(31) + 1;
}
red[i] = temR;
}
}
/**
* @param red
* 一个数组(含有生产的号码)或者是开奖号码
* @param temR
* 进行比较的号码
* @return 在red中是否含有temR
*
* 在生成号码时用来防止 产生相同的号码,在开奖时用来查询重合的号码
*/
private static boolean existe(int red[], int temR) {
// TODO Auto-generated method stub
int i = 0;
while (red[i] != 0 && i < red.length) {
// System.out.println(i);
if (temR == red[i]) {
return true;
}
if (i >= 5)
return false;
i++;
}
return false;
}
/**
* 展示摇取的号码
*/
public void shownumber() {
if (this.name != "drawLottery") {
System.out.print(this.name + "的号码:");
} else
System.out.print("开奖号码:");
for (int i = 0; i < red.length; i++) {
System.out.print(red[i] + " ");
}
System.out.println(blue);
}
/**
* @param drawLottery 开奖号码
* @param e1 摇到的号码
*
* 此函数,根据摇号与开奖号 确定中奖等级
*/
public static void openAward(excel drawLottery, excel e1) {
// TODO Auto-generated method stub
if (drawLottery.name != "drawLottery") {
System.out.println("参数不为开奖号码");
return;
}
int redover = 0;
for (int i = 0; i < 6; i++) {
if (existe(drawLottery.red, e1.red[i])) {
redover++;
}
}
System.out.println(redover);
if (drawLottery.blue != e1.blue && (redover == 0 || redover == 1 || redover == 2 || redover == 3)) {
System.out.println(e1.name + "未中奖");
} else {
System.out.print("恭喜"+e1.name+"中了");
switch (redover) {
case 0: {
if(drawLottery.blue == e1.blue){
System.out.print("六");
}
break;
}
case 1: {
if(drawLottery.blue == e1.blue){
System.out.print("六");
}
break;
}
case 2: {
if(drawLottery.blue == e1.blue){
System.out.print("六");
}
break;
}
case 3: {
if(drawLottery.blue == e1.blue){
System.out.print("五");
}
break;
}
case 4: {
if(drawLottery.blue == e1.blue){
System.out.print("四");
}else{
System.out.print("五");
}
break;
}
case 5: {
if(drawLottery.blue == e1.blue){
System.out.print("三");
}else{
System.out.print("四");
}
break;
}
case 6: {
if(drawLottery.blue == e1.blue){
System.out.print("一");
}else{
System.out.print("二");
}
break;
}
}
System.out.println("等奖!");
}
}
}
- 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
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
activity.java
public class activity {
public static void main(String[] args) {
// TODO Auto-generated method stub
excel e1=new excel("张三");
e1.shownumber();
excel drawLottery=new excel("drawLottery");
drawLottery.shownumber();
excel.openAward(drawLottery,e1);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
输出结果
张三的号码:9 30 26 18 31 16 8
开奖号码:30 24 25 26 15 13 9
2
张三未中奖
上一篇: Java打印1-100内的奇数并求和