Hi.
I'm working on a program that rolls 2 dice a number of times and displays the number of rolls per combination. It also displays this in a bar graph that shows the percentage of times that each combination got rolled. I've gotten to the point where it rolls the dice and gets the values for each combination. But I can't get it to find the average for each combination.
Any help is appreciated :)

``````import java.awt.*;
import hsa.Console;

public class idice
{
static Console i;

public static void main (String[] args)
{
i = new Console ();
int die1, die2, ctr = 0, ctr2 = 2, roll, total = 0, num = 190;
int[] array1 = new int [13];
int avg;

i.println ("Enter the number of rolls.");

for (ctr = 0 ; ctr < 11 ; ctr++)
{
array1 [ctr] = 0;
}

for (ctr = 0 ; ctr < roll ; ctr++)
{
die1 = (int) (Math.random () * 6) + 1;
die2 = (int) (Math.random () * 6) + 1;
//i.println ("dice 1: " + die1 + " dice 2: " + die2 + "");
total = die1 + die2;
array1 [total] += 3;
}

for (ctr = 2 ; ctr < 13 ; ctr++)
{
i.println ("# of " + ctr + "= " + (array1 [ctr2] / 3));
ctr2++;
}

for (ctr = 2, avg = 0 ; num <= 540 ; ctr++)
{
avg = (array1 [ctr] / roll) * 100;
i.println ("avg: " + avg + "");
i.fillRect (num, (450 - array1 [ctr]), 23, avg);
num = num + 35;
}

i.drawString ("100 ---", 155, 153); //153
i.drawString ("90 ---", 160, 183);
i.drawString ("88 ---", 160, 213);
i.drawString ("70 ---", 160, 243);
i.drawString ("60 ---", 160, 273);
i.drawString ("50 ---", 160, 303);
i.drawString ("40 ---", 160, 333);
i.drawString ("30 ---", 160, 363);
i.drawString ("20 ---", 160, 393);
i.drawString ("10 ---", 160, 423);
i.drawString ("0 ---", 160, 453);
i.drawString ("   2          3         4          5         6          7          8          9        10        11        12", 190, 465);
i.drawString ("SUM OF ROLL(S)", 330, 490);
i.drawString ("DICE ROLL(S)", 330, 170);
i.drawLine (180, 150, 180, 450); //y-axis
i.drawLine (180, 450, 562, 450); //x-axis
}
}``````

I'm not sure I understand your problem. You're trying to count the number of times each number was rolled? And find the percentage of the time that each number was rolled? If that is the case, keep an integer array to count the number of times each number was rolled. The indexes can represent the number, and the value stored in that index can represent how many times you rolled that number.

edit: I see you're doing what I suggested, kind of. But keep in mind it is impossible to roll a 0 or a 1 if you're counting both die as the roll.

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.