基本的に、2つのダイスが無作為にロールされ、次に2つのダイス値が加算されます(6と3をロールし、合計が9です)。
合計のロール回数は、totalTally []に格納されます。
のでtotalTally [9] 3.ダイスロールの集計 - 配列の印刷が間違っている
を=ここに私のコードですが、あなたが9の合計をロール当時の3、あなたが10回をロール言う:
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
ここに私の問題だ:それはあなたが2個のダイスを転がしている場合は1をロールバックすることはできません。
そして、12をロールバックすることは可能です。
私のすべての値をシフトする必要があります。
私は間違っていますか?
1を引く必要があるコードを投稿の[MCVE]ガイダンスをお読みください。どのように配列を生成するのか知る方法はないので、 'totalTally [i-1]'の 'i-1'が問題なのかどうかは不明です。 –
どのようにランダムダイスロールを生成していますか? 0..5からの数値を返すjava.util.Random.nextInt(6)を使用している可能性があります。 – Jamie
@StinePikeなぜですか?ループは1 –