import java.util.Random;
import java.util.Scanner;
public class DemoLotteryTicket {
public int[] create() {
int[] arr = new int[7];
Random random = new Random();
for (int i = 0; i < arr.length; ) {
int redNum = random.nextInt(33) + 1;
boolean flag = contains(arr, redNum);
if (!flag) {
arr[i] = redNum;
i++;
}
}
int blueNum = random.nextInt(16) + 1;
arr[arr.length - 1] = blueNum;
return arr;
}
public boolean contains(int[] arr, int num) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == num) {
return true;
}
}
return false;
}
public int[] userInput() {
int[] arr = new int[7];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < arr.length-1; ) {
System.out.println("请输入第" + (i + 1) + "个红球号码:");
int redNum = scanner.nextInt();
if (redNum >= 1 && redNum <= 33) {
boolean flag = contains(arr, redNum);
if (!flag) {
arr[i] = redNum;
i++;
} else {
System.out.println("当前红球号码已经存在,请重新输入");
}
} else {
System.out.println("当前红球号码超出范围");
}
}
System.out.println("请输入蓝球号码");
while (true) {
int blueNum = scanner.nextInt();
if (blueNum >= 1 && blueNum <= 16) {
arr[arr.length - 1] = blueNum;
break;
} else {
System.out.println("当前蓝球号码超出范围");
}
}
return arr;
}
public static void main(String[] args) {
DemoLotteryTicket demoLotteryTicket = new DemoLotteryTicket();
int[] arr = demoLotteryTicket.create();
System.out.println("随机的彩票号码");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
int[] usreInput=demoLotteryTicket.userInput();
System.out.println("用户的彩票号码");
for (int i = 0; i < usreInput.length; i++) {
System.out.print(usreInput[i]+" ");
}
int redCount=0;
int blueCount=0;
for (int i = 0; i < usreInput.length-1; i++) {
int redNum=usreInput[i];
for (int j = 0; j < arr.length-1; j++) {
if (redNum==arr[j]){
redCount++;
break;
}
}
}
int blueNum=usreInput[usreInput.length-1];
if (blueNum==arr[arr.length-1]){
blueCount++;
}
System.out.println();
System.out.println("红球中奖个数:"+redCount);
System.out.println("蓝球中奖个数:"+blueCount);
if(redCount==6&&blueCount==1){
System.out.println("一等奖");
} else if (redCount==6&&blueCount==0){
System.out.println("二等奖");
} else if (redCount==5&&blueCount==0){
System.out.println("三等奖");
}else if (redCount==5&&blueCount==0||redCount==4&&blueCount==1){
System.out.println("四等奖");
}else if (redCount==4&&blueCount==0||redCount==3&&blueCount==1){
System.out.println("五等奖");
}else {
System.out.println("六等奖");
}
}
}