Taylor
Taylor

Reputation: 3

Using Java to print upside down right triangle with Astericks for a lab

I'm currently taking a programming course at my university and I'm stuck on this lab exercise where we have to print this shape:

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

The code I have prints the shape but it's right side up and I can't figure out how to get it in the upside down pattern. here's the code I have typed up

final int max_rows = 10; //The Shape is 10 rows long

for (int row = 1; row <= max_rows; row++)
{
    for (int star = 1; star <= row; star++)
        System.out.print ("*");
    System.out.println();
}

Upvotes: 0

Views: 1075

Answers (1)

GreenSaber
GreenSaber

Reputation: 1148

Try this:

final int max_rows = 10; //The Shape is 10 rows long

    for (int row = max_rows; row > 0; row--)
    {
       for (int star = 1; star <= row; star++)
          System.out.print ("*");

          System.out.println();
       }
    }

I reversed the first for loop, starting with the max_rows and looping until max_rows is 0.

Upvotes: 2

Related Questions