Dice Counter

• January 26th, 2011, 08:40 AM
Xxl0n3w01fxX
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:

Code Java:

```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
• January 26th, 2011, 09:31 AM
samfin
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.
• January 26th, 2011, 09:40 AM
Xxl0n3w01fxX
Re: Dice Counter
Can you explain it in a more newbier form and possible do an example. thx
• January 26th, 2011, 09:54 AM
samfin
Re: Dice Counter
In your loop you are assigning random numbers to Dice1 and Dice2 which works ok.
Code Java:

```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
Code Java:

``` 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.

Code Java:

```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.
• January 26th, 2011, 10:03 AM
Xxl0n3w01fxX
Re: Dice Counter
Thnx This Help Alot

kk another Question:

Code Java:

``` 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.

Code Java:

``` for(int i=0; i < 1 ; i++) { Dice1 = (int)(Math.random()*6) + 1; if(Dice1==1) { nOne++; } Else { (Dice1==2) { nTwo++;```
and so on?
• January 26th, 2011, 10:33 AM
JavaPF
Re: Dice Counter
Quote:

Originally Posted by Xxl0n3w01fxX
Can i add an Else for example.

Code Java:

``` 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?
• January 26th, 2011, 10:35 AM
KevinWorkman
Re: Dice Counter
For what it's worth, this sure does sound like a job for an array or a List.
• January 26th, 2011, 10:49 AM
Xxl0n3w01fxX
Re: Dice Counter
oh ok Thx For the Help Anyways JavaPF