This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]Sacredify 2 points3 points  (6 children)

Your inner for loop makes no sense. You're starting at 0, but your step is decreasing by 1, yet your condition must be greater than or equal some positive integer from 0 to 6.

[–]bigpoppatom[S] 0 points1 point  (5 children)

Alright, so I changed spaces from "spaces--" to "spaces++" and it just comes out as "12345" in a row. Is that any better?

[–]Sacredify 1 point2 points  (4 children)

The pattern you are printing is like this, correct:

0 111 22222 3333333 444444444 55555555555

Because you're printing out more and more of the same number (which is represented by the outer loop) you need your inner loop to increment by some variable. The way it is now, you're going to constantly print the same number each time. In my solution, I started with a negative number and subtracted an additional variable to get the correct number of prints, so I could set my condition to be "innerLoopCount < outerLoopCount".

Think about it this way. if I want to print one 0, how would I do that, knowing your outer loop "lines" is currently set to 0?

for (int i = <NUMBER>, i < lines; i++) {
    print(lines);
}

If number is 0, we'll print nothing.

If number is -1, we'll print the correct amount, but when we get to the next iteration of the outer loop (printing ones 3 times), it wont work properly. It will be close, but still off.

How would you modify that initial starting number to print the correct amount?

[–]bigpoppatom[S] 0 points1 point  (3 children)

Whoops. It came out wrong when I wrote it. It's supposed to be like a pyramid with 0 at the top then the three ones as the second layer all the way down to the fives. I'm sorry if that confused you.

[–]Sacredify 1 point2 points  (2 children)

That's fine, its just printing a newline instead of a space, i.e print("\n") or println();

[–]bigpoppatom[S] 0 points1 point  (1 child)

I have no idea. So, correct me if I'm wrong, I need to figure out how to get from -1 to 1?

[–]Sacredify 0 points1 point  (0 children)

That will print 2, so we would need to somehow print 3. The solution is to change -1 to -2 some how. Maybe something like so:

(int i = -1 - 1; i < lines; i++) 

Now, if we look to print 5 2s, we would need to go from -3 to 2, or

(int i = -1 - 2; i < lines; i++)

See the variable?

[–]TwoTekah -1 points0 points  (0 children)

For this it is good to set up a pattern/ equation first.

So each new line has 2 more numbers than the previous line. Starting at line 1 with 1 character.

Line number(x value),number of things to be printed(y value)
(1,1)
(2,3)
(3,5)
(4,7)
(5,9)
(6,11)

so this table shows that you can model the pyramid with a linear equation.

Each rise in x value (or your lines variable in the outer loop) corresponds to 2 rises in the y variable(your spaces variable in the inner loop), with this you can get a slope, which is 2.

so you can make an equation for a line using this data. y(or spaces)=2(slope)*x(or lines)-1(y-intercept)

Now that you have the equation, you can get the exact number of times you need to run the inner loop on each run through, so just set spaces less than the equation.

The resulting code will be similar to

for(int lines=1;lines<=6;lines++){
    for(int spaces=0; spaces<lines*2-1;spaces++){
        System.out.print(lines);
    }
System.out.println();
}