d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Java Problem > Number Triangle
Add Reply New Topic New Poll
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Feb 18 2014 02:42pm
I have to make a few different triangles using nested loops, but I'm really having trouble with this last one.
It's supposed to look like
__________1
________2 1
______3 2 1
____4 3 2 1
__5 4 3 2 1
6 5 4 3 2 1

(not sure if jsp allows blank spaces leading a line, so using _ instead)

The closest I've been able to get is
__________1
________1 2
______1 2 3
____1 2 3 4
__1 2 3 4 5
1 2 3 4 5 6

The code I have is
Code
for(int i=1; i<=6; i++){
for(int j=6; j>=i; j--)
System.out.print(" ");
for(int j=1; j<=i; j++)
System.out.print(j+" ");
System.out.println();
}


Any help is seriously appreciated, I've been staring at this problem for too long....

This post was edited by furbyjs on Feb 18 2014 02:43pm
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Feb 18 2014 03:22pm
just do this

Code

for (int i = 1; i <= 6; i++) {
for (int j = 6; j >= i; j--)
System.out.print(" ");
for (int j = i; j >= 1; j--)
System.out.print(j + " ");
System.out.println();
}
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Feb 18 2014 03:32pm
Quote (m0hawk @ Feb 18 2014 04:22pm)
just do this

Code
for (int i = 1; i <= 6; i++) {
  for (int j = 6; j >= i; j--)
    System.out.print(" ");
  for (int j = i; j >= 1; j--)
    System.out.print(j + " ");
  System.out.println();
  }


I knew it would be simple, I just didn't know what to change =/
Thank you though, I'm terrible with these things.
Go Back To Homework Help Topic List
Add Reply New Topic New Poll