Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Dice Counter

1. ## Dice Counter

Hi Im am basicly new to java and the these forums and i am having a problem here.
The thing is im still in high school and we started to learn java and we are doing a dice program and what we have to do is that we have to make it roll a pair of six sided die and we also have to make it count how much 1s, 2s, 3s, 4s, 5s, and 6s showed up out of said amount of rolls which is what i am having the problem with

heres my code:

```import java.util.Random;

public abstract class DiceRoll {

public static void main(String[] args) {

int Dice1 = 0;
int Dice2 = 0;

Random generator = new Random();

int counter = 0;
int nOne = 0;
int nTwo = 0;
int nThree = 0;
int nFour = 0;
int nFive = 0;
int nSix = 0;

for(int i=0; i < 1200 ; i++) {
Dice1 = (int)(Math.random()*6) + 1;
Dice2 = (int)(Math.random()*6) + 1;

} // end for

System.out.println("There were " + nOne   + " ones,  " + nTwo   + " twos, "
+ nThree +      " threes, "    + nFour  + " fours, " + nFive  + " fives, and, "
+ nSix   +      " sixs "       + "were rolled out of ");
} // end main

} // end class DiceRoll```

Thank you for help  Reply With Quote

3. ## Re: Dice Counter

The problem is that you're assigning different numbers to Dice1 and Dice2 for each pass of the loop but you don't have any code to increment nOne, nTwo, nThree and the rest so when you print them out, they still have the original 0 value you've assigned. You need to put code in to test the value of Dice1 and Dice2 in each loop so if its 1 you do nOne++ and so on.  Reply With Quote

4. ## Re: Dice Counter

Can you explain it in a more newbier form and possible do an example. thx  Reply With Quote

5. ## Re: Dice Counter

In your loop you are assigning random numbers to Dice1 and Dice2 which works ok.
```for(int i=0; i < 1200 ; i++) {
Dice1 = (int)(Math.random()*6) + 1;
Dice2 = (int)(Math.random()*6) + 1;

}```

The problem is that the loop then runs again and you assign two different random numbers to Dice1 and Dice2 and the original numbers are lost.

You have the variables to count the number of times each number is rolled but these variables are never used. They are assigned a value of 0
```        int nOne = 0;
int nTwo = 0;
int nThree = 0;
int nFour = 0;
int nFive = 0;
int nSix = 0;```

and then not used again so when you print them, they are still 0.

The best way I can see is either if statements or switch statements which can read the current value of Dice1 and add one to the right variable.

```for(int i=0; i < 1200 ; i++) {
Dice1 = (int)(Math.random()*6) + 1;
if(Dice1==1)
{
nOne++;
}
if(Dice1=2)
{
nTwo++;
}```
and so on for Dice1 = 3, 4, 5 and 6
You could also use switch statements, link to a tutorial below.

Switch Statements

You would have to do the check the values for Dice2 as well.  Reply With Quote

6. ## The Following 2 Users Say Thank You to samfin For This Useful Post:

JavaPF (January 26th, 2011), Xxl0n3w01fxX (January 26th, 2011)

7. ## Re: Dice Counter

Thnx This Help Alot

kk another Question:

instead of doing this:

``` for(int i=0; i < 1 ; i++) {
Dice1 = (int)(Math.random()*6) + 1;
if(Dice1==1)
{
nOne++;
}
if(Dice1==2)
{
nTwo++;
}
if(Dice1==3)
{
nThree++;
}
if(Dice1==4)
{
nFour++;
}
if(Dice1==5)
{
nFive++;
}
if(Dice1==6)
{
nSix++;
}```

Can i add an Else for example.

``` for(int i=0; i < 1 ; i++) {
Dice1 = (int)(Math.random()*6) + 1;
if(Dice1==1)
{
nOne++;
}
Else {
(Dice1==2)
{
nTwo++;```
and so on?  Reply With Quote

8. ## Re: Dice Counter Originally Posted by Xxl0n3w01fxX Can i add an Else for example.

``` for(int i=0; i < 1 ; i++) {
Dice1 = (int)(Math.random()*6) + 1;
if(Dice1==1)
{
nOne++;
}
Else {
(Dice1==2)
{
nTwo++;```
and so on?
The code is wrong but I get what you are trying to do. You can't do this because your basically saying, if you roll a 1, Dice1 becomes 1, but if anything else is rolled, Dice1 becomes 2. What if a 3 was thrown?  Reply With Quote

9. ## The Following User Says Thank You to JavaPF For This Useful Post:

Xxl0n3w01fxX (January 26th, 2011)

10. ## Re: Dice Counter

For what it's worth, this sure does sound like a job for an array or a List.  Reply With Quote

11. ## Re: Dice Counter

oh ok Thx For the Help Anyways JavaPF  Reply With Quote