Dice help. posting the number of times a number is rolled.

What I need is a program to roll a die 10000 times and count the number of 1s rolled, 2s rolled, etc.. After that I need the program to use JOptionPane to post the total tally for each of the rolls in one window.

so far this is what I have:

Code:

`import javax.swing.*;`

02

public class Dice

03

04

{

05

public static void main(String[] args)

06

{

07

08

09

int[] A = new int[10000];

10

11

for(int i=0; i<A.length; i++)

12

A[i] = 1 + (int) (6*Math.random());

13

14

for(int i=0; i<A.length; i++)

15

System.out.println(A[i]);

16

17

}

18

}

What I need now is to somehow tally the numbers and post them in the JOptionPane window. I'm pretty sure I need to create a string but I don't know where to start.

Re: Dice help. posting the number of times a number is rolled.

Why do you have a 10000 long array? Do you plan on keeping every number rolled? If so why? I think a better idea would be to have an array to store the counts of each number rolled, if it is 1 increment the 1 count, if it was 2 increment the 2 count etc.

Re: Dice help. posting the number of times a number is rolled.

Quote:

Why do you have a 10000 long array? Do you plan on keeping every number rolled? If so why? I think a better idea would be to have an array to store the counts of each number rolled, if it is 1 increment the 1 count, if it was 2 increment the 2 count etc.

Well the assignment calls for a total of 10,000 rolls, but if there is an easier way to do this while still using Math.random and the 10,000 rolls i'm all ears.

Re: Dice help. posting the number of times a number is rolled.

I've got similar program to make... i'll link u to a post about it:

http://www.java-forums.org/new-java/...m-2-cubes.html

hope it helps!

Re: Dice help. posting the number of times a number is rolled.

Make an array of size x, where x is the number of sides on the die. From there you increment the correct slot in the array each time you roll. Junky hinted at this as well. Give it a shot and see how it works out.

Re: Dice help. posting the number of times a number is rolled.

Your program only requires how many times something has been rolled. IE for a dice your options are 1,2,3,4,5,6. That means your array only needs to be 6 slots.

Run your Math.Random, then do a logical statement to detect what digit has been rolled. Assign each array slot a integer variable initialized to zero and assign it to a number corresponding to a side on the die. Then increment the variable in the array slot every time the side of the die is rolled.

Simple really, just got to put your noggin into it. You may also be able to use a primitive in the array too, depending on what you plan on doing after the dice are rolled. If all you are doing is rolling, storing and then print after 10,000 rolls then use a primitive.

Re: Dice help. posting the number of times a number is rolled.

Quote:

Originally Posted by

**cookiejarvus** Well the assignment calls for a total of 10,000 rolls, .

Where did I say "Don't roll 10,000 times"? What I did say was don't store those 10,000 rolls. Only store the current roll and 6 counts (one for each number). Just like you would on paper if I were to call out the 10,000 rolls to you.

Re: Dice help. posting the number of times a number is rolled.

Ok so while I was in class the other day I asked some peers if they had any input. They told me that I need to include a do/while method. I took that input and tried to build off of it. So far I've managed to put together this jumble of nonfunctional code.

Code:

`import javax.swing.*;`

public class DiceW

{

public static void main(String[] args)

{

int num1, num2, num3, num4, num5, num6;

int num1 = 0;

int num2 = 0;

int num3 = 0;

int num4 = 0;

int num5 = 0;

int num6 = 0;

int total = 10000;

String choice="";

do

{

int[] A = new int[10000];

for(int i=0; i<A.length; i++)

A[i] = 1 + (int) (6*Math.random());

for(int i=0; i<A.length; i++)

{

dice = (int)(Math.random90*6+1);

sum = (dice);

switch (sum)

{

case 1 : num1++;

case 2 : num2++;

case 3 : num3++;

case 4 : num4++;

case 5 : num5++;

case 6 : num6++;

break;

}

System.out.println(A[i]);

JOptionPane.showMessageDialog(null, "The dice has been rolled ten-thousand times, here is the statistics" +

"\nThe number of 1s" + "=" + num1 +

"\nThe number of 2s" + "=" + num2 +

"\nThe number of 3s" + "=" + num3 +

"\nThe number of 4s" + "=" + num4 +

"\nThe number of 5s" + "=" + num5 +

"\nThe number of 6s" + "=" + num6);

choice = JOptionPane.showInputDialog("Continue?");

} while(choice.equals("yes") | choice.equals("yes"));

}

}

}

The Choice to continue at the end is one of the requirements of the assignment so i'm pretty sure that I have to use the do/while method and build off that. Hope that makes sense. I only get 4 errors when compiling so I think im on the right track.

Re: Dice help. posting the number of times a number is rolled.

I still do not understand why you want a 10,000 long array to store all the numbers. You need to revise switch statements. If you are getting error messages then you should post the exact messages here since we do not read minds.

Re: Dice help. posting the number of times a number is rolled.

You honestly don't even need switch statements. Here is some pesudocode:

Code:

`declare array of length n //n is the number of sides a die has`

loop

increment a random element in the array

end loop

The whole thing should be possible in ~ 10 lines of code.

Re: Dice help. posting the number of times a number is rolled.

Quote:

Originally Posted by

**sunde887** You honestly don't even need switch statements. .

Yep that was my advice in the first reply but it seems all advice is being ignored.

Re: Dice help. posting the number of times a number is rolled.

Its not that your advice is being ignored its that I do not understand java therefore, I do not know how to follow your advice. You need to be really specific with me and include examples otherwise, I have no clue what your talking about.

as far as the errors I receive they are as follows:

I:\Java\Project 2\DiceW.java:63: error: while expected

}

^

I:\Java\Project 2\DiceW.java:66: error: illegal start of expression

}

^

I:\Java\Project 2\DiceW.java:66: error: reached end of file while parsing

}

^

I:\Java\Project 2\DiceW.java:67: error: reached end of file while parsing

4 errors

Tool completed with exit code 1

Re: Dice help. posting the number of times a number is rolled.

When you declare an array you specify the length as well

Code:

`int[] x = new int[50];`

In this case, the array x would have a length of 50.

Since there are only 6 sides on the die you will only need an array which is 6 ints long. After creating this, start a loop which loops 10,000 times and increment the correct spot based on a random number. When indexing into an array you are able to index with the return value of Random.nextInt(...)

Re: Dice help. posting the number of times a number is rolled.

Quote:

Originally Posted by

**cookiejarvus** Its not that your advice is being ignored its that I do not understand java

In my first post I advised that you cahnge the 10,000 long array to one that was only 6 long. Yet in your code you have not done that. Since you have managed to declare an array then the only conclusion I can arrive at is that you understand enough Java to do this and therefore my advice is being ignored. If at any time you did not understand the point I was trying to make then ask don't ignore and continue stumbling along without a clue.