Hello and good evening everyone :)

I've written up a program that counts the number of times each number on a die (dice) is rolled, with the percent. Only thing is, I'm not quite sure how to output the percent. Everything else seems to be working except that part... What am I missing?

Here's what I have so far:

``````import javax.swing.JOptionPane;

public class CountRandom
{
public static void main (String[] args)
{

String numberString = JOptionPane.showInputDialog(null,
"Please input number of times to roll:",
"Rolling Dice", JOptionPane.QUESTION_MESSAGE);

int n = Integer.parseInt(numberString);

int[] dieCount = new int[7];

for (int i = 0; i < n; i++)
{
int roll = rollOne();
recordRoll (dieCount, roll);
}

printResults (dieCount, n);
}

public static int rollOne()
{
int result = 1 + (int)(Math.random()*6);

return result;
}

public static void recordRoll(int[] countArray, int roll)
{
if (roll == 1)
countArray[1] = countArray[1] + 1;
else if (roll == 2)
countArray[2] = countArray[2] + 1;
else if (roll == 3)
countArray[3] = countArray[3] + 1;
else if (roll == 4)
countArray[4] = countArray[4] + 1;
else if (roll == 5)
countArray[5] = countArray[5] + 1;
else if (roll == 6)
countArray[6] = countArray[6] + 1;
}

public static void printResults (int[] dCount, int numRolls)
{

for (int index = 1; index < 7; index++)
{

System.out.printf ("%d occurred %d times -> %4.2f %%\n", index, dCount[index], 0.0);
}
}

}``````

Thanks!

For each possible outcome, you want to print out what its percentage was. You have everything you need to calculate that. You can store the number of dice rolls in a variable (or hand to the method, although it would be better to store). Then for each result you are printing, you have the total (stored in that variable) and the number of times it had that result. Those are the only two things you need to calculate the percentage. It's all mathematics from there on...

Black Box

Since your method already has the number of rolls, you're all set to calc the percentage. You do have to watch out for the integer division though or you'll get 0 for every one of them

``(dCount[index]/(float)numRolls)*100f``

A couple of other minor points:
You don't really need 7 elements in your array. You are only using six.

With a six element array, this entire count function

``````if (roll == 1)
countArray[1] = countArray[1] + 1;
else if (roll == 2)
countArray[2] = countArray[2] + 1;
else if (roll == 3)
countArray[3] = countArray[3] + 1;
else if (roll == 4)
countArray[4] = countArray[4] + 1;
else if (roll == 5)
countArray[5] = countArray[5] + 1;
else if (roll == 6)
countArray[6] = countArray[6] + 1;``````

reduces to

``countArray[roll-1]++;``

There a couple of other minor index changes you'll need to make in your output, but I think those will be pretty clear :)

Ahh, perfect - I fixed it! Always thankful for the push in the right direction. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.