Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with using nested loops!

    Hello, I am trying to make an application that displays a pattern of asterisks a certain way. the only output statements I could use are to get the pattern below. My question would be which way to use the nested loop, (do i do while(if else)...

    System.out.print( "*" );
    System.out.println();
    System.out.print( " " );

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

    Code:
    public class Triangles
    {
    	public void drawTriangles()
    	{
    		int row;
    		
    		int column;
    		
    		int space;
    		
    		while ( row <= 1 )
    		{
    						
    			System.out.print( "*" );
    			
    			row++;			
    		
    		}
    	}
    
    }
    Last edited by ventil; 05-07-2009 at 04:49 AM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Moved to Java forum.

    Oh, yeah. I'm not a mod. Hopefully one will.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    ventil - This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Ask a mod to move this over.

  • #4
    New to the CF scene
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yea I am sorry i will.

  • #5
    New Coder
    Join Date
    Mar 2006
    Posts
    23
    Thanks
    0
    Thanked 2 Times in 2 Posts
    I don't truly understand the question, but if I wanted to create that triangle with asterix's using a nested loop it'd look like this:

    Code:
    maxX = 10;
    
    for( x = 0; x < maxX; x++ ) {
        for( y = 0; y < x; y++ ) print( "*" );
        print( "<br>" );
    }
    Last edited by Sphynx; 05-07-2009 at 03:13 PM.

  • #6
    New to the CF scene
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The question is confusing sorry, the problem with that is I cant set a value to any of the variables, I was also wondering if an array would work in this situation,

  • #7
    New Coder
    Join Date
    Mar 2006
    Posts
    23
    Thanks
    0
    Thanked 2 Times in 2 Posts
    int x; while( x < 11 ) { int y; while( y < x ) print( "*" ); }?

  • #8
    New to the CF scene
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok this is a homework assignment, but I dont want anyone to do it for me, all i want is a hint on how to start it.

    the triangle should look like this :

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

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


    Code:
    public class Triangles
    {
    	public void drawTriangles()
    	{
    		int row;
    		
    		int column;
    		
    		int space;
    		
    		//first triangle
    		/* Write code to display the first triangle.
    		  Use nested for loops.
    		  The outer loop should control which row of
    		  asterisks is being displayed. The inner loop
    		  should display one asterisk at a time. */
    		  
    		  //Second triangle
    		  /*Write code to display the third triangle using
    		   techniques similar to the first triangle*/
    		
    	
    	
    	}
    
    }
    I have no idea on how to start the for loop, because the compiler gives me an error saying that the variable hasnt been initialized which is true when i dont give it a value, I guess i dont really understand how the application is supposed to work but the template doesnt show any of the variables having a value, is there another way that can be done without setting any values?

  • #9
    New to the CF scene
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    and the only output statements i can use are

    System.out.print( "*" );
    System.out.println();
    System.out.print( " " )

  • #10
    New Coder
    Join Date
    Mar 2006
    Posts
    23
    Thanks
    0
    Thanked 2 Times in 2 Posts
    The first loop I gave you will work in that class and do as you wish (so will the 2nd one, I just forgot the <br> (or println()) in there.

    Just replace the x with row, the y with column, the print with your System.out.print, the print( "<br>" ) with the println(). Just play with it some.

  • #11
    New to the CF scene
    Join Date
    May 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well i tried that but like i said because the variable arent declared the compiler is giving me an error saying that variable have not been initialized


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •