# Help with Loop

• 10-14-2010, 03:37 AM
PsychoNaut
Help with Loop
Hello, I really could use some help with this mini-assignment, im halfway through the semester with all A's on my projects(no help), but this one really makes me feel dumb :(

"Write a class TwoDice with a static method rollTimes(int n) that returns the average(as a double) of the points when two dice are rolled independently n times. You do that by using the expression above twice and add the results into an "accumulator" variable. At the end divide the accumulated sum by n but watch out for the fine distinction between integer
and normal division."

(int)(Math.random()*6)+1 = 1, 6 sided dice roll

Heres the code:

public class TwoDice
{
public static double rollTimes(int n)

{

int diceRoll1=(int)(Math.random()*6) + 1;
int diceRoll2=(int)(Math.random()*6) + 1;
int sum=0;

for(int i=n; i>=0; i--)
{
}

double div = (double)(sum/n);
return div;

}

}

It outputs zero repeatedly..

I know there must be something wrong with my loop logic.. Can I pass the variable input n through the loop?

Any hints is appreciated.
• 10-14-2010, 05:11 AM
Fubarable
You create a pair of random numbers once and once only rather than each time the dice is rolled, and the logic in the for loop is farked up beyond belief (sorry for the bluntness). Solution: 1) Get two random numbers with each roll and 2) figure out your program logic before writing code. The logic isn't hard, and if you figure it out on paper before trying to code it, it will come to you. Seriously, it will.
• 10-14-2010, 06:01 AM
PsychoNaut
Ok will do thanks.