I need to use a nested for loop in Java to make a triangle like this
********
*******
******
*****
****
***
**
*
Heres my code:
for (int i=8; i>0; i--)
{
for (int j=0; j<i; j++)
{
System.out.print('#');
}
System.out.println("");
}
I get a triangle but not the one i want. Instead, my triangle looks like this:
********
*******
******
*****
****
***
**
*
You'll need the outer loop to count the 8 rows. The inner loop would output the *'s for each row. The row count of the outer loop will tell you how many spaces to output versus *'s.
Use the following code
Your code was also producing an unnecessary line at the end of the triangle, this code takes care of that line and is capable of making the desired triangle.
I have tested it, see here.
Try this