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 add= diceRoll1+diceRoll2;
for(int i=n; i>=0; i--)
double div = (double)(sum/n);
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.
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.