Click here to Skip to main content
16,018,158 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I did a square where the is a diagonal going from top left to right but how do I do it from right to left?
Example of what i did:
\ x x x
x \ x x
x x \ x
x x x \

Example of what i need:
\ x x \
x \ \ x
x \ \ x
\ x x \

What I have tried:

System.out.println("Please enter the length of the side: ");
             
             int side =kb.nextInt();
             for (int i = 1; i <= side; i++ ) 
		{
			for (int j = 1; j < i; j++ ) 
			{
				System.out.print("X ");
			}
Posted
Updated 13-Oct-22 16:08pm
v2
Comments
Member 8428760 12-Oct-22 10:00am    
The first diagonal has a variable x. The other diagonal will be 4-x where 4 is the length of the line.

1 solution

I'm just guessing, but what you really need is this, isn't it:
\ x x /
x \ / x
x / \ x
/ x x \

And there is a simple way to do it, with just two loops and a couple of ifs.

Think about what you are doing: for each row you print, the column with the same number as the row should be a '/'.
And the column with the same number as the N minus the row number should be a '\', where N is the number of rows (minus one if you start with row and column zero).
All the other characters are 'X'.

So have a loop for each row, and a nested loop for each column in the row, and use an if ... else if ... else inside the loop to decide which character to print.

Try it on paper and you'll see what I mean.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900