Results 1 to 4 of 4
- 04-06-2012, 09:31 PM #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(); } }
-
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.
- 04-06-2012, 09:43 PM #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];
- 04-06-2012, 09:52 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Image Not Displaying
By mDennis10 in forum New To JavaReplies: 3Last Post: 02-26-2012, 01:23 AM -
How to add leading zeroes to int
By pink123 in forum New To JavaReplies: 11Last Post: 03-01-2011, 07:18 PM -
Add zeroes to int
By pink123 in forum Advanced JavaReplies: 1Last Post: 03-01-2011, 05:57 PM -
help with saving bleeding zeroes.
By SteroidalPsycho in forum New To JavaReplies: 6Last Post: 04-14-2010, 08:55 AM -
Shift Off Trailing Zeroes
By nwboy74 in forum New To JavaReplies: 5Last Post: 02-25-2010, 07:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks