[Solved] Need to calculate the average from a loop java


You can simplify

die1 = (Math.random()*6)+1;
die1 = (int) die1;

To

die1 = (int) ((Math.random() * 6) + 1);

But you don’t need to cast it to an integer if you want exact values, thats going to truncate (get rid of) your decimal places.

To sum all rolls,

int total = 0;
int maxRolls = Integer.MAX_VALUE; //lol
for (int roll = 1; roll <= maxRolls; roll++) {
    // roll your dice
    total += (die1 + die2);
}

To Average

double average = (total / maxRolls);

To Output

System.out.println("Total: " + total + " Average: " + average + " Rolls: " + maxRolls);

You’re welcome, now do your own homework

solved Need to calculate the average from a loop java