Results 1 to 2 of 2
- 09-22-2011, 03:39 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 1
- Rep Power
- 0
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( " " ); } } } }
- 09-22-2011, 09:47 AM #2
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
-
My code was not executed properly.It will jumping to exception handling.my code is
By vinay4051 in forum EclipseReplies: 3Last Post: 08-10-2011, 09:17 AM -
servlet include method copying sorce code and executing source code as output how to
By shamkuma2k in forum Advanced JavaReplies: 0Last Post: 08-07-2011, 08:32 PM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks