Page 2 of 2 FirstFirst 12
Results 21 to 23 of 23
  1. #21
    basla is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

    Default

    I actually did figure it out on my own before his response, but I really appreciate his help. It was good to look and see the solution he came up with vs what I did and realize there are many ways to solve it.

    I do actually like it though that you all don't typically spoon feed answers. I know I learn better when I have to figure it out. Some guidance is much appreciated though.

    In short, thanks again to all!

    Here's what I came up with for my code. It has a ton of documentation notes to myself explaining what I'm doing, so sorry if that makes it hard to read:
    Java Code:
     
    
    public class TrianglePrinting //name of program
    {
    	//Main method begins execution of Java program
    	public static void main(String[] args) 
    	{ 
    		//first triangle         
    	 	for(int i=1; i<=10; i++)/*declare and initialze control 
    		variable; set loop continuation condition; increment control 
    		variable by 1*/ 
    			  {
    			  	for(int j=1; j<=i; j++)/*declare and initialze control 
    				variable; set loop continuation condition; increment 
    				control variable by 1*/
    				System.out.print("*");//display * to screen
        			System.out.println(" ");//display white space to screen		
    				}//end for loop
    		System.out.println("\n");//print new line
    
    		//second triangle
    		for(int i=10; i>=0; i--)/*declare and initialze control 
    		variable; set loop continuation condition; decrement control 
    		variable by 1*/
    				{
    				for(int j=1; j<=i; j++)/*declare and initialze control 
    				variable; set loop continuation condition; increment 
    				control variable by 1*/
    				System.out.print("*");//display * to screen
    				System.out.println(" ");//display white space to screen	
    				}
    		//end for loop
    		System.out.println("\n");//print new line
    	
    		//third triangle
    		int nLines = 10;//declare and initialize nLines
    
    		for (int i = 1; i <= nLines; i++)/*declare and initialize 
    		control variable; set loop continuation condition; Increment 
    		control variable by 1.*/
    			{
    			int nSpaces = i - 1;/*initiate nSpaces to one less than the 
    			numberof lines*/
    			int nStars = (nLines - i) + 1;/*initiate asterisks to the 
    			number of lines*/
    
    				for (int j = 1; j <= nSpaces; j++)/*declare and initialze 
    				control variable; set loop continuation condition; increment 
    				control variable by 1*/
    				System.out.print(" ");//display white space to screen
    					
    					for (int j = 1; j <= nStars; j++)/*declare and initialze 
    					control variable; set loop continuation condition; 
    					increment control variable by 1*/
    					System.out.print("*");//display * to screen
    					System.out.println("");//display white space to screen
    			}//end for loop
    			System.out.println(" \n");//print new line
    			
    		//fourth triangle 
    		for (int i = 1; i <= nLines; i++)/*declare and initialize control 
    		variable; set loop continuation condition; Increment control 
    		variable by 1.*/
    			{
    			int nSpaces = (nLines - i) + 1;/*declare and initialize nSpaces 
    			to one less than the numberof lines*/
    			int nStars = i - 1;//initiate asterisks to the number of lines
    
    			for (int j = 1; j <= nSpaces; j++)/*declare and initialze control 
    			variable; set loop continuation condition; increment control 
    			variable by 1*/
    			System.out.print(" ");//display white space to screen
    				
    				for (int j = 1; j <= nStars; j++)/*declare and initialze 
    				control variable; set loop continuation condition; increment 
    				control variable by 1*/
    				System.out.print("*");//display * to screen
    			
    				System.out.println("");//display white space to screen
    			}//end for loop
    			
    	}//End main method			
    }//End class
    ----jGRASP exec: java TrianglePrinting
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********


    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *



    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *



    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********

  2. #22
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I am glad you solved it. There will always be more than one way to solve anything. If you wanted you could probably do this in much more complicated ways. The more you work with it, the easier the logic will become. If you get really stuck, come here and ask for help as a last resort. This is how I have been learning java. TBH, I didn't even know how to declare arrays in the beginning of January.

    If you are satisfied with the help you have received here, please mark the thread solved with the thread tools at the top.

  3. #23
    basla is offline Member
    Join Date
    Mar 2011
    Posts
    13
    Rep Power
    0

Page 2 of 2 FirstFirst 12

Similar Threads

  1. JFrame problem - need suggestions
    By mystxx in forum Advanced Java
    Replies: 2
    Last Post: 06-19-2010, 10:29 PM
  2. While loop problem
    By mochibon in forum New To Java
    Replies: 3
    Last Post: 04-18-2010, 08:21 PM
  3. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  4. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 07:16 PM
  5. Suggestions required for solving a Java problem
    By bilal_ali_java in forum Advanced Java
    Replies: 3
    Last Post: 08-16-2008, 01:11 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
  •