Java彩票填充数组

时间:2015-02-13 03:35:36

标签: java arrays fill

以下是代码:

 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));
    }

}

通过这种方法,我想玩这么多玩家数量的抽奖,直到玩家数量和彩票数量的每个指数相同。我想将每个结果保存在winsNum中,这样我就可以看到有多少次我点击0,1,2,3等等。现在它只播放一次并将其存储在数组中。我尝试使用循环和布尔值多次播放,但我无法让它工作

1 个答案:

答案 0 :(得分:0)

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();
    }

我认为这就是你要找的东西,如果你想要一些解释,请发表评论 - 请注意,winsNum数组大小为6 - 因为你可以得到0-4正确的每个分级+最终你获胜的地方有6个选项