This is the example we were given.
Write a Java class that simulates rolling dice. The
class should contain two methods, roll and main, as follows:
❑ int roll() – This method should use the Random class
from the Java library to simulate rolling a die by generating a
random number. It should return the value rolled, an integer
from 1 to 6 (don’t print here) [Look up Random in the Java docs!]
❑ main – This method should simulate rolling the die 20 times.
It should call the roll method 20 times and print out each roll.
It should also calculate & print the average roll as a double.
❑ Do not make the roll method static
▪ Create an object variable of your class in the main method to call the roll method.
I know that this should be so simple but I am struggling with it so bad.
This is what I have so far and when I run it, it just prints the number 5 once. Im not sure what is going on here because even when I change the range of the random method, only a 5 prints out.
public class DieRoll {
public int roll(int randomNumber){
for (int i = 0; i < 20; i++){
randomNumber =(int)(Math.random()* 6)+ 1;
}
return randomNumber;
}
public static void main(String[] args) {
DieRoll dieOne = new DieRoll();
dieOne.roll(20);
}
}