0

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
    }
}

}

janice
  • 3
  • 2

2 Answers2

1

First of all, your for loop is off by 1:

for (int i = 0; i <= 36000; i++) {
//                 ^
// remove this "=" or you will loop 36001 times

Your sum seems redundant, so remove that as well.

I think you just don't know how to format the output so that the floats correct to 2 d.p. right?

It's easy. Just add do %.2f!

Your printf will be like:

System.out.printf("%d\t%d\t%.2f\n",i,frequency[i], percentage);

Another problem with your code is that it might produce unaligned stuff. It also does not align the values to the right as the sample output shows. To fix this, you also just need to change the printf. Like this:

System.out.printf("%3d\t%9d\t%10.2f\n",i,frequency[i], percentage);

If you want to read more about how printf works, list here.

Sweeper
  • 145,870
  • 17
  • 129
  • 225
0

Stealing from this answer: we can use padRight which is defined as:

public static String padRight(String s, int n) {
    return String.format("%1$-" + n + "s", s);
}

and do:

System.out.printf("Sum\tFrequency\tPercentage\n");
for (int i = 2; i < frequency.length; i++) {
    String s = padRight(String.valueOf(i), 4);
    String f = padRight(String.valueOf(frequency[i]), 12);
    percentage = (frequency[i] * 100.0) / 36000;
    String p = String.format("%.2f", percentage); // This formatting will keep two digits after the point
    System.out.printf("%s%s%s\n", s ,f, p);
}

OUTPUT (example)

Sum Frequency   Percentage
2   992         2.76
3   2031        5.64
4   3034        8.43
5   3947        10.96
6   4887        13.58
7   5948        16.52
8   4965        13.79
9   4051        11.25
10  3014        8.37

You can play with it and remove the \t from the first line and use spaces instead in order to create the amount of spacing you want between the columns and then pass the second parameter in the calls to padRight accordingly.

Nir Alfasi
  • 49,889
  • 11
  • 75
  • 119