# Thread: Can anyone see why this is displaying all zeroes?

1. Member
Join Date
Mar 2012
Posts
16
Rep Power
0

## Can anyone see why this is displaying all zeroes?

I am working on a Dice Roll simulator. For some reason in my DiceTester class (the class I'm using to roll the dice and report what I find), the numbers aren't going into the array like I expect them to. Anybody see the problem?

Java Code:
```public class DiceTester{

int x;
Dice dice;

//Initializes Dice roll totals and doubles

int[] diceTotalList = new int[13];
int[] doubleList = new int[7];

//runs a test for "n" rolls of the dice.

public void runTest(int n){

for(int a = 0; a > n ; ++a){

int diceRoll = dice.rollDice();
int doubleRoll = dice.doubles();

for(int x = 0; x > n; ++x){

if(diceRoll == 2 && doubleRoll == 0){
++diceTotalList[2];
}
else if(diceRoll == 3 && doubleRoll == 0){
++diceTotalList[3];
}
else if(diceRoll == 4 && doubleRoll == 0){
++diceTotalList[4];
}
else if(diceRoll == 5 && doubleRoll == 0){
++diceTotalList[5];
}
else if(diceRoll == 6 && doubleRoll == 0){
++diceTotalList[6];
}
else if(diceRoll == 7 && doubleRoll == 0){
++diceTotalList[7];
}
else if(diceRoll == 8 && doubleRoll == 0){
++diceTotalList[8];
}
else if(diceRoll == 9 && doubleRoll == 0){
++diceTotalList[9];
}
else if(diceRoll == 10 && doubleRoll == 0){
++diceTotalList[10];
}
else if(diceRoll == 11 && doubleRoll == 0){
++diceTotalList[11];
}
else if(diceRoll == 12 && doubleRoll == 0){
++diceTotalList[12];
}
else if(diceRoll == 13 && doubleRoll == 0){
++diceTotalList[13];
}
else{
if(doubleRoll == 1){
++doubleList[1];
}
else if(doubleRoll == 2){
++doubleList[2];
}
else if(doubleRoll == 3){
++doubleList[3];
}
else if(doubleRoll == 4){
++doubleList[2];
}
else if(doubleRoll == 5){
++doubleList[5];
}
else if(doubleRoll == 6){
++doubleList[6];
}

}
}
}
} //end of runTest

public void report(){

System.out.printf("The number 2 was rolled %d times.\n", diceTotalList[2]);
System.out.printf("The number 3 was rolled %d times.\n", diceTotalList[3]);
System.out.printf("The number 4 was rolled %d times.\n", diceTotalList[4]);
System.out.printf("The number 5 was rolled %d times.\n", diceTotalList[5]);
System.out.printf("The number 6 was rolled %d times.\n", diceTotalList[6]);
System.out.printf("Doubles were rolled %d times\n", doubleList[1] + doubleList[2] + doubleList[3] + doubleList[4] + doubleList[5] + doubleList[6]);

}

//}

public static void main(String[] args){

DiceTester diceTester = new DiceTester();
diceTester.runTest(100000);
diceTester.report();

}
}```

2. ## Re: Can anyone see why this is displaying all zeroes?

The problem may be in the code you've not shown us, the Dice class.

3. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
780
Rep Power
4

## Re: Can anyone see why this is displaying all zeroes?

1. u never create a dice object!
2. your for loops-conditions are wrong - read a book
for(int a = 0; a > n ; ++a){
->
for(int a = 0; a < n; ++a){
The same here for(int x = 0; x > n; ++x){
(and the declared x in the class header is unused now!)
3. the whole if statements are unnecessary!
++diceTotalList[diceRoll];

4. Member
Join Date
Mar 2012
Posts
16
Rep Power
0

## Re: Can anyone see why this is displaying all zeroes?

Originally Posted by eRaaaa
1. u never create a dice object!
2. your for loops-conditions are wrong - read a book
for(int a = 0; a > n ; ++a){
->
for(int a = 0; a < n; ++a){
The same here for(int x = 0; x > n; ++x){
(and the declared x in the class header is unused now!)
3. the whole if statements are unnecessary!
++diceTotalList[diceRoll];
I had noticed and fixed all the other errors before I looked at your comment, but I now understand why I should use ++diceTotalList[diceRoll] instead and my counters work perfectly. Thanks!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•