I need my program to display the expected and actual percentages of how often the sums of two dice appear. I can't get the expected percentages to display the array, it always comes out as 0. I am not sure how to calculate the actual percentages using the random values. The sum and tally part work fine. I also keep getting a "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11 at Dice.main(Dice.java:30)" error at the end.
public class Dice {
public static void main(String[] args) {
int [] counters=new int[13]; //array of counters
int [] expected={1/36,2/36,3/36,4/36,5/36,6/36,5/36,4/36,3/36,2/36,1/36};
int [] actual=new int[13];
//initialize array values to 0
for(int i=1; i<counters.length; i++) {
counters[i]=0;
}
for(int i=1; i<actual.length; i++) {
actual[i]=0;
}
//generating die values and updating counter
for(int i=1; i<=36000; i++) {
int d1=(int) (Math.random()*6)+1;
int d2=(int) (Math.random()*6)+1;
int sum= d1+d2;
counters[sum]++;
}
//display table
System.out.printf("%10s %10s %15s %15s \n", "Sum:", "Tally:", "Expected Percentage (%):", "Actual Percentage (%):");
for(int i=2; i<counters.length; i++) {
System.out.printf("%8d %10d %10d %20d\n", i, counters[i], expected[i], actual[i]);
}
}
}
I expect the output to show as decimals or as fractions, but I only get zeros.
Aucun commentaire:
Enregistrer un commentaire