Nested For Loop Square- Java

isabel8998

I am new to Java and I'm working on making a square of two triangles using a nested for loop. It is supposed to look like this:

+ + + + + 
* + + + + 
* * + + + 
* * * + + 
* * * * + 

My code looks like this so far:

for (int i = 0; i < 5; i++) {             
for (int j = 0; j < i; j++) {               
System.out.print("* ");             
}  
System.out.println();

but all it gives me is the star section of the square. I have no idea how to go from here. Can anyone help?

alexwbt
for (int x = 0; x < 5; x++) {
    for (int y = 0; y < 5; y++) {
        System.out.print(x > y ? "* " : "+ ");
    }
    System.out.println();
}

You are probably going for something like this?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related