代码如下:
public class Demo02 {
static String[] sysNumber = new String[] { "", "", "", "", "", "", "" };
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请选择号码投注");
print();
redBall();
System.out.println("请输入投注号码以每个号码以空格隔开");
String betNum = sc.nextLine();
String regex = "[ ]";
String[] person = betNum.split(regex);
contrast(sysNumber,person);
System.out.println("系统开奖号码为:");
print(sysNumber);
// print(person);
}
public static void contrast(String[] sys,String[] person){//判断中了几等奖
int number = 0;
for (int i = 0; i < sys.length-1; i++) {
for (int j = 0; j < person.length-1; j++) {
if(person[j].equals(sys[i])){
number++;
}
}
}
if(sys[sys.length-1].equals(person[person.length-1])){
String num = "";
switch(number){
case 6:
num = "一";
break;
case 5:
num = "二";
break;
case 4:
case 3:
num = "三";
break;
case 2:
case 1:
num = "四";
break;
default:
break;
}
System.out.println("恭喜你获得"+num+"等奖");
}else
System.out.println("对不起您没有中奖");
}
public static void print() { //遍历可选球号
System.out.println("红球区选择6个球,每个号码在同一注中只能选一次");
for (int i = 1; i <= 33; i++) {
System.out.print(" "+numIf(i));
if (i % 11 == 0) {
System.out.println();
}
}
System.out.println("蓝球区可选择一个号码");
for (int i = 1; i <= 16; i++) {
System.out.print(" "+numIf(i));
if(i%8==0){
System.out.println();
}
}
}
public static void redBall() {// 系统开出号码获取
Random r = new Random();
String s = "";
for (int i = 0; i < sysNumber.length - 1; i++) {
int n = r.nextInt(33) + 1;
if (judeg(sysNumber,n)) {
s = numIf(n);
sysNumber[i] = s;
} else
i--;
}
int n = r.nextInt(16) + 1;
s = numIf( n);
sysNumber[sysNumber.length-1] = s;
}
public static String numIf(int n) {//把9以的数字前面加上0,并把所胡数字转成字符串
String s;
if (n < 10) {
s = "0" + n;
} else {
s = "".valueOf(n);
}
return s;
}
public static boolean judeg(String[] sys,int n) {// 判断int随机娄是否重复
for (int i = 0; i < sys.length; i++) {
if (!"".equals(sys[i])) {
if (Integer.valueOf(sysNumber[i]) == n) {
return false;
}
}
}
return true;
}
public static void print(String[] s) {
for (int i = 0; i < s.length; i++) {
System.out.print(s[i] + " ");
}
System.out.println();
}
}
|