2017-04-11 41 views
0

基本上,兩個骰子隨機擲出一堆,然後將兩個骰子值加在一起(您擲出6和3,總數爲9)。
總共滾動​​了多少次的頻率存儲在totalTally []中。
所以說你滾10次,那你滾一共有9次3,totalTally [9] = 3Tallying骰子卷 - 陣列打印錯誤

這裏是我的代碼:

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.
所以我所有的值都需要移位。

我在做什麼錯?

+3

請張貼代碼閱讀[MCVE]指導。沒有辦法知道你如何生成數組,因此不清楚'totalTally [i-1]'中的'i-1'是否是一個問題或其他問題。 –

+2

你如何生成隨機骰子卷?很可能您正在使用返回0..5的數​​字的java.util.Random.nextInt(6)。 – Jamie

+1

@StinePike爲什麼?循環從1 –

回答

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

爲什麼減去2?

你應該減1,而不是

totalTally[roll[i][0] + roll[i][1]-1]++ 
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]); 
    }