I am having trouble getting the percentage of the frequency to print. Below is the question:
Write a program to simulate the rolling of two dice. The program should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums. Your application should roll the dice 36,000 times. Use a one dimensional array to keep track of the number of times each possible sum appears. Display the results in tabular format. Determine whether the totals are reasonable (e.g., here are six ways to roll a 7, so approximately one-sixth of the rolls should be 7). Sample output:
Sum Frequency Percentage
2 1027 2.85
3 2030 5.64
4 2931 8.14
5 3984 11.07
6 5035 13.99
7 5996 16.66
8 4992 13.87
9 4047 11.24
10 2961 8.23
11 1984 5.51
12 1013 2.81
This is my code so far:
import java.util.Random;
public class dice_roll {
public static void main(String [] args){
Random rand = new Random();
int dice1, dice2;
int [] frequency = new int [13];
int [] rolls = new int [13];
int sum;
double percentage;
for (int i = 0; i <= 36000; i++) {
dice1 = rand.nextInt(6)+1;
dice2 = rand.nextInt(6)+1;
frequency[dice1+dice2]++;
sum = dice1 + dice2;
}
System.out.printf("Sum\tFrequency\tPercentage\n");
for (int i = 2; i < frequency.length; i++) {
percentage = (frequency[i] * 100.0) / 36000;
System.out.printf("%d\t%d\t\n",i,frequency[i]);//this line here
}
}
}