Results 1 to 2 of 2
  1. #1
    shirrokurro is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Default Can this code be refined a bit more?

    I'm wondering if the code below can be refined a bit. I feel that some parts of it seems odd just looking at it. I know there are many ways to solve a problem. The comments before each for loop is the output of the code. I've studied Java a long time ago and I'm picking it back up again because I've forgotten too much Thanks in advance.

    Java Code:
    public class PrintingPatterns {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            //Pattern 1
            //1 
            //1 2 
            //1 2 3 
            //1 2 3 4 
            //1 2 3 4 5 
            //1 2 3 4 5 6 
            for ( int i = 1; i <= 6; ++i )
            {
                for ( int j = 1; j <= i; ++j )
                {
                    System.out.print( j + " " );
                }
                
                System.out.println();
            }
            
            System.out.println();
            
            //Pattern 2
            //1 2 3 4 5 6 
            //1 2 3 4 5 
            //1 2 3 4 
            //1 2 3 
            //1 2
            //1 
            for ( int i = 0; i <= 6; ++i )
            {
                for ( int j = 1; j <= 6 - i; ++j )
                {
                    System.out.print( j + " " );
                }
                
                System.out.println();
            }       
         
            //Pattern 3
            //          1
            //        2 1
            //      3 2 1
            //    4 3 2 1
            //  5 4 3 2 1
            //6 5 4 3 2 1 
            for ( int i = 6; i >= 1; --i )
            {
                for ( int j = 1; j <= i - 1; ++j )
                {
                    System.out.print( "  " );
                }
                
                for ( int k = 6; k >= i; --k )
                {
                    System.out.print( ( k - i + 1 ) + " " );
                }
                System.out.println();
            }
            
            System.out.println();
            
            //Pattern 4
            //1 2 3 4 5 6
            //  1 2 3 4 5
            //    1 2 3 4
            //      1 2 3
            //        1 2
            //          1
            for ( int i = 6; i >= 1; --i )
            {
                for ( int j = 1; j <= i; ++j )
                {
                    System.out.print( j + " " );
                }
                
                System.out.println();
                
                for ( int k = 6; k >= i; --k )
                {
                    System.out.print( "  " );
                }
            }
        }
    }

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default Re: Can this code be refined a bit more?

    I think you must select a common part and do an one method, which will do all functions. Because you have two loop every time. Just change iteration condition.
    Skype: petrarsentev
    http://TrackStudio.com

Similar Threads

  1. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  2. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  3. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  4. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •