计数骰子卷 - 阵列打印错误

时间:2017-04-11 16:03:24

标签: java arrays

基本上,两个骰子随机滚动一堆,然后将两个骰子值加在一起(你滚动一个6和一个3,你的总数是9)。
滚动总次数的频率存储在totalTally []中 所以说你滚10次,其中3次你总共滚动9次,总计Tally = 9。

这是我的代码:

import java.util.Random;

public class dice
{
    public static void main(String a[])
    {

        System.out.println("Creating arrays to store information. This might take a while.");
        int[][] tally = new int[6][6]; //Creates an array called tally to keep track of how many times each number was rolled. [Dice1][Dice2]
        int[] totalTally = new int[12]; //Creates an array called totalTally to keep track of frequency of totals rolled. 
        int[][] roll = new int[36000000][2]; //Creates an array to store dice roll info.
        System.out.println("Rolling two 6-sided dice " + roll.length + " times...");
        Random r = new Random(); //Creates a new random number generator
        for (int i = 0; i < roll.length; i++) //For loop that rolls the dice to fill the length of the array.
        { 
            roll[i][0] = (r.nextInt(6) + 1); //Assigns random number between 1 and 6 to the array for dice 1 result.
            roll[i][1] = (r.nextInt(6) + 1); //Assigns random number between 1 and 6 to the array for dice 2 result.
            tally[roll[i][0]-1][roll[i][1]-1]++; //Increments by 1 the respective result in the tally array.
            totalTally[roll[i][0] + roll[i][1]-2]++; //Increments by 1 the respective result in the totalTally array.
        }
        System.out.println("All done. Results are below.");
        //Following lines print first table header
        System.out.println("\n ROLL SUMMARY:");
        System.out.println("Dice 1 + Dice 2         Frequency");
        System.out.println("---------------------------------");
        for (int i = 1; i <= totalTally.length; i++)//for loop goes through totalTally values
        {
            System.out.println("         " + i + "               " + totalTally[i-1]);
        }
        //Following lines print second table header
        System.out.println("\n DETAILED VIEW:");
        System.out.println("Dice 1      Dice 2      Total       Frequency");
        System.out.println("---------------------------------------------");
        for (int j = 1; j <= 6; j++) //For  loop goes through dice 1 values
        {
            for (int k = 1; k <= 6; k++) // Nested for loop goes through dice 2 values
            {
                System.out.println(j + "           " + k + "           " + (j+k) + "           " + tally[j-1][k-1]); //Prints line for table with dice values
            }
        }
    }
}

这是我得到的第一个包含该代码的表的输出:

Dice 1 + Dice 2         Frequency

1                    998639
2                    1997209
3                    2998118
4                    4000336
5                    4999210
6                    6001277
7                    5001144
8                    4000794
9                    3002596
10                    2001501
11                    999176
12                    0

这是我的问题:如果你掷两个骰子,就不可能掷1 并且可以滚动12 所以我的所有价值都需要转移。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

这就是你“统一”单个骰子的方式。

Random r = new Random();
int[] die1 = new int[5]; //Creates an array to tally die 1    
for (int roll = 0; roll < totalRolls; roll++) {
    die1[r.nextInt(6)] += 1;  // increment the index of the random roll
}

为每个骰子做那个。

您的总计数似乎会计算个人

for (int side = 1; side <= 6; side++) {
    System.out.printf("%d\t%d\n", side, die1[side-1] + die2[side-1]);
}

如果您想计算总数,请务必在2开始打印,而不是1

    System.out.println("Dice 1 + Dice 2         Frequency");
    System.out.println("---------------------------------");
    /// See here 
    for (int i = 1; i <= totalTally.length; i++)//for loop goes through totalTally values
    {
        System.out.println("         " + i + "               " + totalTally[i-1]);
    }

答案 1 :(得分:1)

totalTally[roll[i][0] + roll[i][1]-2]++

为什么减去2?

你应该减去1而不是

totalTally[roll[i][0] + roll[i][1]-1]++