Results 1 to 4 of 4
  1. #1
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default 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. #4
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

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

    Quote Originally Posted by eRaaaa View Post
    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!

Similar Threads

  1. Image Not Displaying
    By mDennis10 in forum New To Java
    Replies: 3
    Last Post: 02-26-2012, 01:23 AM
  2. How to add leading zeroes to int
    By pink123 in forum New To Java
    Replies: 11
    Last Post: 03-01-2011, 07:18 PM
  3. Add zeroes to int
    By pink123 in forum Advanced Java
    Replies: 1
    Last Post: 03-01-2011, 05:57 PM
  4. help with saving bleeding zeroes.
    By SteroidalPsycho in forum New To Java
    Replies: 6
    Last Post: 04-14-2010, 08:55 AM
  5. Shift Off Trailing Zeroes
    By nwboy74 in forum New To Java
    Replies: 5
    Last Post: 02-25-2010, 07:56 AM

Posting Permissions

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