Hi guys,
This week Ive been wrecking my head trying to write a code to run the following program:
This program must test the dice 50 times. Then the results will be recorded and summarised. For each number (1 to 6), the program will print the most times that number occurred, the fewest times it occurred and the average.
The program output would look like:
How many times do you want to throw the dice:
6000
Number Average Most Fewest
1: 1001.3 1011 987
2: 998.3 1002 999
3: 1001.8 1020 987
4: 1000.9 1010 983
5: 997.9 1009 981
6: 999.3 1015 987
Basically, the program will ask the user how many times they want to roll a dice, then that dice will be rolled and the amount of times it lands on a number will be recorded i.e 6 occurred 20 times etc.
Then this code will be tested 50 times and when that is done it will print the maximum times a number was recorded, the minimum times and the average for all 50 tests.
So far I managaed to write a code that will roll the dice and record the amount of times a certian number appears but I have no clue how to even try the next part.
I am to try and use methods and arrys for this code but I just cant seem to think of anything.
Here is my code so far
import java.util.Random;
public class RandomDie
{
public static void main(String [] args)
{
System.out.println("How many times do you want to roll the dice?");
int y = Console.readInt();
int x = 6 + 1;
Random generator = new Random();
int[] count = new int[x];
for(int i = 1; i < y ; i++)
{
int d = generator.nextInt(x);
count[d]++;
}
for(int p = 1; p < x; p++)
{
System.out.println(p + " " + count[p]);
}
}
}
Any ideas or hints would be greatly appreciated guys.
Thanks so much in advance.
-David