Results 1 to 6 of 6
  1. #1
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default Counting problem in java

    Hi! I am a beginner in java.
    Now I am writing a program about counting.

    ArrayOfDice is an array with 5 elements that each element wound be an integer from 1 to 6. Imagine that this array contains 5 dice.

    NoOfDiceArray is an array with 6 elements that each element wound be an integer. And, the element is used to store the amount of different facevalue of the five dice. Let's say, NoOfDiceArray[0] is used to store the amount of "ONE", NoOfDiceArray[1] is used to store the amount of "TWO"................NoOfDiceArray[5] is used to store the amount of "SIX".
    For example, if the five dice are (1, 2, 1, 3, 6), NoOfDiceArray wound contains {2, 1, 1, 0, 0, 1}.

    And I use switch statment to do the counting but there is a bug.
    Use the example above, if the five dice are (1, 2, 1, 3, 6), NoOfDiceArray wound contains {2, 1, 1, 0, 0, 1}.
    But the actual output is {2, 2, 3, 0, 0, 6}.

    if the five dice are (6, 6, 6, 6, 6), the output becomes {0, 0, 0, 0, 0, 30}!!!!!

    I have no idea about this bug. Could you please help me to find out the core problem? Thank you.

    Here is my code.

    public class Counting
    {
    public int[] NoOfDiceArray = new int[6];

    public Counting()
    {
    int[] NoOfDiceArray = {0, 0, 0, 0, 0, 0};
    }

    public int[] Count(int[] ArrayOfDice)
    {
    for (int i = 0; i < ArrayOfDice.length; i++)
    {
    switch(ArrayOfDice[i])
    {
    case 1:
    NoOfDiceArray[0]++;
    break;
    case 2:
    NoOfDiceArray[1]++;
    break;
    case 3:
    NoOfDiceArray[2]++;
    break;
    case 4:
    NoOfDiceArray[3]++;
    break;
    case 5:
    NoOfDiceArray[4]++;
    break;
    case 6:
    NoOfDiceArray[5]++;
    break;
    }
    }
    return NoOfDiceArray;
    }

    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Ouch. You must have posted the wrong code, that works, but read the Code Conventions regarding the names you use Code Conventions for the Java Programming Language

    What is your test harness, as that code is not runnable?

    And use CODE tags when posting code. That is: [CODE]... your code goes here ... [/CODE]
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default

    Thz phHein.
    Actually I use Test.java to test the Counting.java.

    In Test.java, I use TestArray6 = {6, 6, 6, 6, 6} to test the Counting.java.
    My expected result is "Amount of Six: 5" but the actual output is "Amount of Six: 30"

    Counting.java
    Java Code:
     
    
    public class Counting 
    {
    	public int[] NoOfDiceArray = new int[6];
    	
    	public Counting()
    	{
    		int[] NoOfDiceArray = {0, 0, 0, 0, 0, 0};
    	}
    
    	public int[] Count(int[] ArrayOfDice)
    	{
    		for (int i = 0; i < ArrayOfDice.length; i++)
    		{
    			switch(ArrayOfDice[i])
    			{
    				case 1:
    					NoOfDiceArray[0]++;
    					break;
    				case 2:
    					NoOfDiceArray[1]++;
    					break;
    				case 3:
    					NoOfDiceArray[2]++;
    					break;
    				case 4:
    					NoOfDiceArray[3]++;
    					break;
    				case 5:
    					NoOfDiceArray[4]++;
    					break;
    				case 6:
    					NoOfDiceArray[5]++;
    					break;
    			}
    		}
    		return NoOfDiceArray;
    	}
    	
    }
    Test.java
    Java Code:
    public class Test
    {
    	public static void main (String[] args)
    	{
    		Counting A;
    		A = new Counting();
    		
    		int[] TestArray1 = {1, 1, 1, 1, 1};
    		int[] TestArray2 = {2, 2, 2, 2, 2};
    		int[] TestArray3 = {3, 3, 3, 3, 3};
    		int[] TestArray4 = {4, 4, 4, 4, 4};
    		int[] TestArray5 = {5, 5, 5, 5, 5};
    		int[] TestArray6 = {6, 6, 6, 6, 6};
    		
    		System.out.println ("Amount of One:   " + A.Count(TestArray6)[0] +
    							"\nAmount of Two:   " + A.Count(TestArray6)[1] +
    							"\nAmount of Three: " + A.Count(TestArray6)[2] +
    							"\nAmount of Four:  " + A.Count(TestArray6)[3] +
    							"\nAmount of Five:  " + A.Count(TestArray6)[4] +
    							"\nAmount of Six:   " + A.Count(TestArray6)[5]);
    		
    	}
    }

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Man, you're calling Count() 6 times with TestArray6 and never reset your counter!! 5*6 = 30
    Your program works.

    And do us all a favour and read the code conventions!
    Last edited by PhHein; 07-07-2010 at 03:06 PM.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default

    I solved it!!
    Thank you.

  6. #6
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 03:03 PM
  2. Counting help
    By jksmithson in forum New To Java
    Replies: 1
    Last Post: 11-06-2009, 02:43 AM
  3. Need help with counting letters
    By mrdestroy in forum New To Java
    Replies: 15
    Last Post: 10-22-2008, 01:33 PM
  4. [SOLVED] Help on Word and Character counting in java
    By Alistair in forum New To Java
    Replies: 2
    Last Post: 05-15-2008, 03:48 AM
  5. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 05:51 PM

Posting Permissions

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