1.输入我们的幸运号码,确保输入的数字不重复 对我们输入的号码进行排序
Scanner input = new Scanner(System.in);
IndexBox<Integer> box1 = new LinkedBox<>();
int blue = 0;
System.out.println("请输入红球: ");
a:
for (int i = 1; i <= 6; ) {
System.out.println("请输入【" + i + "】个红球:");
try {
int num = input.nextInt();
if (num < 1 || num > 32) {
System.out.println("输入的号码超过范围,重新输入:");
continue;
}
for (int j = 0; j < box1.size(); j++) {
if (box1.get(j) == num) {
System.out.println("号码重复,重新输入:");
continue a;
}
}
box1.add(num);
i++;
} catch (InputMismatchException e) {
System.out.println("输入的数字非法,重新输入:");
input.next();
continue;
}
}
System.out.println("请输入蓝球: ");
while (true) {
try {
int num = input.nextInt();
if (num < 0 || num > 18) {
System.out.println("输入的数字超过范围");
continue;
}
blue = num;
break;
} catch (InputMismatchException e) {
System.out.println("输入数字非法");
input.next();
continue;
}
}
System.out.println("你输入的号码是: ");
showCodes(box1, blue);
2.构建在新定义的数组中将红色球和蓝色的方法 并对拿出的球号进行排序
public static void showCodes(IndexBox box, int blue) {
int[] boxs = new int[box.size()];
for (int i = 0; i < box.size(); i++) {
boxs[i] = (int) box.get(i);
}
Arrays.sort(boxs);
for (int box1 : boxs) {
System.out.print(box1 + "\t");
}
System.out.println("+" + blue);
}
3.随机产生中奖号码,确保数字不重复,并对其进行排序
IndexBox<Integer> box2 = new LinkedBox<Integer>();
Random r = new Random();
b:
for (int i = 1; i <= 6; ) {
int num = r.nextInt(32) + 1;
for (int j = 0; j < box2.size(); j++) {
if (box2.get(j) == num) {
continue b;
}
}
box2.add(num);
i++;
}
// int blue1=1;
int blue1 = r.nextInt(18) + 1;
System.out.println("本期的中奖号码是:");
showCodes(box2, blue1);
4.对比两组号码,确定最终是否中奖,计算红球相同的数量,篮球是否相同
int red = 0;
for (int i = 0; i < box1.size(); i++) {
if (box1==box2) {
red++;
}
}
if (red == 6 && blue == blue1) {
System.out.println("一等奖!!");
} else if (red == 6) {
System.out.println("二等奖!!");
} else if (red == 5 && blue == blue1) {
System.out.println("三等奖!!");
} else if ((red == 5) || (red == 4 && blue == blue1)) {
System.out.println("四等奖!!");
} else if ((red == 4) || (red == 3 && blue == blue1)) {
System.out.println("五等奖!!");
} else if (blue == blue1) {
System.out.println("六等奖!!");
}else {
System.out.println("未中奖!");
}
}
最终结果: