Hello, im having some trouble with my java assignment.

The Q:
Write a program which will test that a dice is random, i.e. that it is equally likely that any number will appear. E.G The program simulates throwing the dice 6,000 times. The one appears 1003 times, the two, 996 times, etc. Each of the numbers comes up approximately 1000 times, which is what you would expect.

My code so far is as follows:

import java.util.Random;

public class RandomDice 
{
	public static void main(String [] args)
	{
		Random randomNumbers = new Random();

		int die1;
		int die2;
		int [] totals = new int[6];

		for(int index = 0; index < totals.length; index++)
		    totals[index]=0;

		for(int roll = 1; roll <= 5; roll++)
		{
			die1 = 1 + randomNumbers.nextInt(6);
			die2 = 1 + randomNumbers.nextInt(6);
			totals[roll] = die1 + die2;
		}

		System.out.print("How many times do you want to throw the dice: " + totals );

		System.out.println(totals);

		for (int k = 2; k < totals.length; k++)
		{
			System.out.println(k + totals[k]);
		}
	}
}

When i compile and run the program, this is what i get http://gyazo.com/197559d76628cd4b3daa19463f2b03ac.png

Any help appreciated guys, thx.

  1. Line 13 - you are initializing the totals[] array to 0, so far so good.
  2. Line 16 - you want to fill all the cell in the totals[] array again, only now you choose from 1 to 5, missing the first element.
  3. Line 27 - still want to go over all the cells in totals[], now you go from 2.
  4. Regarding the weird text that you are watching after the "How many times do you want to throw the dice" - it is because you are printing the reference totals and not totals.length which is what you want.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.