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!