Results 1 to 4 of 4
- 02-14-2012, 08:01 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Please review my code for using ArrayList, for loops and arrays.
Hi everyone, and thanks for the time..Java Code:package aPackage; import java.util.ArrayList; public class ThreeOfAKindCalculator { static void calculate(ArrayList<Integer> arrayToBeCalculated) { int[] calculator = new int[7]; int calculatedvalue = 0; for(int i = 0;i<5;i++) { int k = arrayToBeCalculated.get(i); calculator[k] = calculator[k] + 1; } for(int i = 0;i<7;i++) { System.out.print(calculator[i] + " "); } System.out.println(); for(int i = 0;i<7;i++) { if(calculator[i]>2) for(int m = 0;m<5;m++) { calculatedvalue = calculatedvalue + arrayToBeCalculated.get(m); } } System.out.println(calculatedvalue); } }
What I do here is, in a TestClass, I have a method that rolls 5 dice and stores the values in an ArrayList.
If there are 3 same values, then I want to know the sum of the dice...
If experienced users can review this code and make suggestions, I would be grateful.
Thank you.
Sample outputs:
[4, 4, 5, 4, 4]
0 0 0 0 4 1
21
[6, 6, 3, 6, 1]
0 1 0 1 0 0 3
22
I want to know if there are any obvious, very easy ways that I am missing.
Just trying to learn..
Thanks.
- 02-14-2012, 10:08 PM #2
Re: Please review my code for using ArrayList, for loops and arrays.
Does your code work correctly or is its output incorrect?
If the output is incorrect, please explain and show what the output should be.
- 02-14-2012, 10:10 PM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Please review my code for using ArrayList, for loops and arrays.
The output is ok.
- 02-14-2012, 10:12 PM #4
Similar Threads
-
Requesting Code Review
By Zeramat in forum New To JavaReplies: 9Last Post: 06-16-2011, 05:59 AM -
[Semi-Beginner] (nested loops) What's wrong with my code? (nested loops)
By Solarsonic in forum New To JavaReplies: 20Last Post: 03-22-2011, 04:02 AM -
How to use arrays and loops?
By asadzarrar in forum New To JavaReplies: 1Last Post: 10-31-2010, 11:04 PM -
please review the following code
By ajbj in forum New To JavaReplies: 3Last Post: 08-25-2009, 08:37 AM -
[SOLVED] Code review
By saeedsubedar in forum Advanced JavaReplies: 14Last Post: 06-25-2008, 05:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks