I wrote code for an assignment that will output 4 triangles, 1 next to the other. It uses the While and nested For loops. The problem is the output is ok for the 1st and 4th triangle but the 2nd and 3rd need to be mirror images of what the output is now. This is the code I have for the assignment:

public class Triangle
{
public static void main(String args[])
{
int k=1;
int m=11;
while ( k !=11 && m !=1 )
{
for ( int i=1; i<=k; i++ )
{
System.out.print ("*");
}
System.out.print("     ");
for ( int i=1; i+1<=m; i++)
{
 System.out.print ("*");
}
System.out.print ("      ");
for ( int i=1; i+1<=m; i++ )
{
System.out.print ("*");
}
System.out.print ("    ");
for ( int i=1; i<=k; i++ )
{
System.out.print ("*");
}
System.out.println();
k++;
m--;
}
}
}

Thanks for your help. David

What are the triangles supposed to look like. I'm not quite sure if you're trying to get numbers or what. But just put down the exact problem question and I'll help you out. I'm doing this in class also for Intro to Programming.

Nevermind the last comment, I plugged in your code and saw what you meant.

I think I found one way. I just changed a little bit around and it got the 1st and 2nd to be mirrors, but flipped over the x-axis then y-axis. I said axis because I wasnt sure how else to put it. How did you want the triangles to be mirrored?

Nevermind the last comment, I plugged in your code and saw what you meant.

I think I found one way. I just changed a little bit around and it got the 1st and 2nd to be mirrors, but flipped over the x-axis then y-axis. I said axis because I wasnt sure how else to put it. How did you want the triangles to be mirrored?

Thanks for your help.....I want the 2nd and 3rd triangles to be mirrored over the x axis or horizontal. So in other words from left to right not up and down.

For the triangles to be mirrored over the x-axis, it seems you'd have to find out how much of a space is needed to have the left side of the triangle be straight. So instead of having a set amount of space, (" "), you'll need to add a certain amount each time. Or possibly the \t escape sequence (which is a tab). tab is used in the parenthesis of a print.

System.out.print("\t\t");

This is a challenging problem to get to work correctly. The stars are written from left to right on a line, then the last println is what brings everyonething one line down. You could switch the k's and m's n the for loops, and it will change the design a little, but it will be hard to get the right spacing between the triangles to get the exact mirror image you're looking for. Sorry I can't help much more. Good luck.

This is a challenging problem to get to work correctly. The stars are written from left to right on a line, then the last println is what brings everyonething one line down. You could switch the k's and m's n the for loops, and it will change the design a little, but it will be hard to get the right spacing between the triangles to get the exact mirror image you're looking for. Sorry I can't help much more. Good luck.

my teacher said it was challenging and she is giving extra credit if we can get it right. Thanks for giving it a try. I may have to change around the for loops to get what I want.

This article has been dead for over six months. Start a new discussion instead.