Results 1 to 4 of 4
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Please review my code for using ArrayList, for loops and arrays.

    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);
    	}
    }
    Hi everyone, and thanks for the time..

    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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default 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.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Please review my code for using ArrayList, for loops and arrays.

    The output is ok.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: Please review my code for using ArrayList, for loops and arrays.

    Comments on the coding:
    Use the length of an array to control the loops, not a hardcoded literal
    Add comments to describe why each section of code is there and what it is supposed to do.

Similar Threads

  1. Requesting Code Review
    By Zeramat in forum New To Java
    Replies: 9
    Last Post: 06-16-2011, 05:59 AM
  2. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  3. How to use arrays and loops?
    By asadzarrar in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 11:04 PM
  4. please review the following code
    By ajbj in forum New To Java
    Replies: 3
    Last Post: 08-25-2009, 08:37 AM
  5. [SOLVED] Code review
    By saeedsubedar in forum Advanced Java
    Replies: 14
    Last Post: 06-25-2008, 05:25 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
  •