首页 > 其他分享 >模拟双色球

模拟双色球

时间:2022-12-24 18:00:51浏览次数:34  
标签:redCount return arr int 双色球 ++ && 模拟

案例需求

1.手动自选双色球号码

public static int[] xuan() {
    int[] arr = new int[7];
    Scanner sc = new Scanner(System.in);
    for (int i = 0; i < 6; i++) {
        System.out.println("请输入第" + (i + 1) + "个红球");
        int number = sc.nextInt();
        if (number >= 1 && number <= 33) {
            if (isExsist(number, arr)) {
                System.out.println("红球重复输入,请重新输入");
                i--;
            } else {
                arr[i] = number;
            }
        } else {
            System.out.println("第" + (i + 1) + "个红球,范围有误,请重新输入");
            i--;
        }

    }
    while (true) {
        System.out.println("请输入第7个蓝球");
        int number = sc.nextInt();
        if (number >= 1 && number <= 16) {
            arr[6] = number;
            break;
        } else {
            System.out.println("第7蓝球,范围有误,请重新输入");
        }
    }

    return arr;
}  

2.查看红球是否已经选择过

public static boolean isExsist(int redball, int[] arr) {
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == redball) {
            return true;
        }
    }
    return false;
}  

3.系统随机产生一组数(双色球开奖号码)

public static int[] sui() {
    Random random = new Random();
    int[] arr = new int[7];
    int[] redpool = new int[33];
    int[] bluepool = new int[16];
    for (int i = 0; i < 33; i++) {
        redpool[i] = i + 1;
    }

    for (int i = 0; i < 16; i++) {
        bluepool[i] = i + 1;
    }

    for (int i = 0; i < 6; i++) {
        int index = random.nextInt(redpool.length);
        arr[i] = redpool[index];
        redpool = getNewArray(index, redpool);
    }
    int index2 = random.nextInt(bluepool.length);
    arr[6] = bluepool[index2];

    return arr;
}

4.根据索引返回新数组(红球池)

public static int[] getNewArray(int index, int[] arr) {
    int[] brr = new int[arr.length - 1];
    for (int i = 0; i < brr.length; i++) {
        if (i < index) {
            brr[i] = arr[i];
        } else {
            brr[i] = arr[i + 1];
        }
    }
    return brr;
}

5.兑奖

public static String dui(int[] xuan, int[] sui) {
    int redCount = 0;
    int blueCount = 0;
    if (xuan[6] == sui[6]) {
        blueCount++;
    }
    //删除蓝球后的sui
    int[] newArray = getNewArray(6, sui);
    for (int i = 0; i < 6; i++) {
        if (isExsist(xuan[i], newArray)) {
            redCount++;
        }
    }
    if (redCount == 6 && blueCount == 1) {
        return "一等奖";
    } else if (redCount == 6 && blueCount == 0) {
        return "二等奖";
    } else if (redCount == 5 && blueCount == 1) {
        return "三等奖";
    } else if ((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 1)) {
        return "四等奖";
    } else if ((redCount == 4 && blueCount == 0) || (redCount == 3 && blueCount == 1)) {
        return "五等奖";
    } else if (redCount < 3 && blueCount == 1) {
        return "六等奖";
    } else {
        return "谢谢惠顾";
    }
}  

6.打印球的内容

public static void printQiu(int[] arr) {
    System.out.print("[");
    for (int i = 0; i < arr.length; i++) {
        System.out.print(i != arr.length - 1 ? arr[i] + ", " : arr[i]);
    }
    System.out.println("]");
}

标签:redCount,return,arr,int,双色球,++,&&,模拟
From: https://www.cnblogs.com/OKGOsky/p/17003112.html

相关文章