Inverting the Triangle using the java for loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vykkilynn
    New Member
    • Mar 2007
    • 1

    #1

    Inverting the Triangle using the java for loops

    My homework this week involved using the for statement to create loops that would display triangles. The only output we're allowed to use are forms of the print statements... i.e. print(), println(). I've gotten my code to display the first 2 triangles I need, but I cannot manage to "invert" the triangles for the second part...

    I need to create code that will make a triangle look like V

    *********
    *******
    *****
    ***
    **
    *
    using ( ' ' ) to produce x number of spaces before displaying the asterisks. my code for my working Triangle is
    Code:
     public class Triangle 
    
    {
    	
    	    public static void main( String[] args ) {
    
            for( int x=0; x<=10; x++ )
            	{
                for( int y=10; y>=x; y-- ) {
                    System.out.print( "*" );
                    }
                
                System.out.println( "" );
            }
        }
    }
    If someone could just help me get it turned around the right way I would appreciate it very very much.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by vykkilynn
    My homework this week involved using the for statement to create loops that would display triangles. The only output we're allowed to use are forms of the print statements... i.e. print(), println(). I've gotten my code to display the first 2 triangles I need, but I cannot manage to "invert" the triangles for the second part...

    I need to create code that will make a triangle look like V

    *********
    *******
    *****
    ***
    **
    *
    using ( ' ' ) to produce x number of spaces before displaying the asterisks. my code for my working Triangle is
    Code:
    public class Triangle 
     
    {
     
    	 public static void main( String[] args ) {
     
    for( int x=0; x<=10; x++ )
    	{
    for( int y=10; y>=x; y-- ) {
    System.out.print( "*" );
    }
     
    System.out.println( "" );
    }
    }
    }
    If someone could just help me get it turned around the right way I would appreciate it very very much.


    Code:
     
    int spaces = 0;
      int starsNum = 10;
      String stars = "***********";
      for( int x = 0; x < starsNum; x++) {
       for(int i = 0; i < spaces; i++) {
    	//print spaces
       }
       //print stars
       //remove 2 stars from the strng stars
       //increment stars by 1
      }

    Comment

    Working...