Results 1 to 6 of 6
Thread: Counting problem in java
- 07-07-2010, 02:15 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 36
- Rep Power
- 0
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;
}
}
- 07-07-2010, 02:31 PM #2
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
- 07-07-2010, 02:52 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 36
- Rep Power
- 0
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
Test.javaJava 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; } }
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]); } }
- 07-07-2010, 03:04 PM #4
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
- 07-07-2010, 03:36 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 36
- Rep Power
- 0
I solved it!!
Thank you.
- 07-07-2010, 03:46 PM #6
Member
- Join Date
- Jul 2010
- Posts
- 36
- Rep Power
- 0
Similar Threads
-
Counting numbers up and down
By radio in forum New To JavaReplies: 4Last Post: 05-06-2011, 03:03 PM -
Counting help
By jksmithson in forum New To JavaReplies: 1Last Post: 11-06-2009, 02:43 AM -
Need help with counting letters
By mrdestroy in forum New To JavaReplies: 15Last Post: 10-22-2008, 01:33 PM -
[SOLVED] Help on Word and Character counting in java
By Alistair in forum New To JavaReplies: 2Last Post: 05-15-2008, 03:48 AM -
Counting Pixels
By shaungoater in forum Java 2DReplies: 5Last Post: 11-29-2007, 05:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks