Java 彩票填充数组

Java lottery fill array

代码如下:

 public void playMany() {

    for (int i = 0; i < lotteryNumbers.length; i++) {
        lotteryNumbers[i] = 0 + r.nextInt(9);

    }


    for (int i = 0; i < playersNumbers.length; i++) {

        playersNumbers[i] = 0 + r.nextInt(9);

    }

    for (int i = 0; i < lotteryNumbers.length; i++) {
        if (lotteryNumbers[i] == playersNumbers[i]) {
            winNum++;

        }
    }
   if (winNum == 0) {
        winningNum[0]++;
    }
    if (winNum == 1) {
        winningNum[1]++;
    }
    if (winNum == 2) {
        winningNum[2]++;
    }
    if (winNum == 3) {
        winningNum[3]++;
    }
    if (winNum == 4) {
        winningNum[4]++;
    }
    if (winNum == 5) {
        winningNum[5]++;
    }
   System.out.println(Arrays.toString(winningNum));
    }

}

使用这种方法,我想用这么多 playerNumbers 玩一次彩票,直到 playersNumbers 和 lotteryNumbers 中的每个索引都相同。我想将每个结果都保存在 winningNum 中,这样我就可以看到我击中了 0、1、2、3 等次数。现在它只播放一次并将其存储在数组中。我尝试使用循环和布尔值来播放很多次,但我无法让它工作

try{
        int[] lotteryNumbers = new int[5];
        int[] playersNumbers = new int[5];
        int[] winningNum = new int[]{0, 0, 0, 0, 0, 0};
        int winNum = 0;
        Random r = new Random();
        for (int i = 0; i < lotteryNumbers.length; i++){
                lotteryNumbers[i] = 0 + r.nextInt(9);
        }
        do{
            winNum = 0;

            for (int i = 0; i < playersNumbers.length; i++){
                playersNumbers[i] = 0 + r.nextInt(9);

            }

            for (int i = 0; i < lotteryNumbers.length; i++){
                if (lotteryNumbers[i] == playersNumbers[i]){
                    winNum++;
                }
            }
            winningNum[winNum]++;
        }while(winNum != 5);
        System.out.println(Arrays.toString(winningNum));
    }catch(Exception e){
        e.printStackTrace();
    }

我认为这就是您要查找的内容,如果您需要其中的一些解释,请发表评论 - 请注意 winningNum 数组的大小为 6 - 因为您可以为每个分层获得 0-4 正确 +您获胜的最后一个留下 6 个选项