0

Printing numbers in pyramid form.

I want to print out this with Java and don't want to use array 0 012 0123 01234 012345

7th Jan 2018, 4:03 PM
Olagoke Tobi
Olagoke Tobi - avatar
15 Antworten
+ 3
It goes something like this: https://code.sololearn.com/WSSA1Ep4116a/?ref=app (run the code) PS. Please don't judge that code, I'm not a web/js type of guy.
7th Jan 2018, 7:19 PM
deFault
+ 4
// With one loop int height = 5, i = 0; String s = ""; do System.out.println(s += i); while (i++ < height);
7th Jan 2018, 5:47 PM
Luc Hariman Randrianomenjanahary
Luc Hariman Randrianomenjanahary - avatar
+ 3
This doesn't need arrays. What you need is two nested loops: outer one goes from zero to height, inner one — from zero to current value of the outer loop counter. Inside the inner loop print the inner loop counter, just after the inner loop print newline.
7th Jan 2018, 4:13 PM
deFault
+ 3
Your inner loop counter increment isn't right. Instead of i=j there should be i++. Here's complete snippet, you almost got it right anyway: int height = 5; for (int i = 0; i < height; i++) { for (int j = 0; j < i; j++) { print(j); } print("\n"); }
7th Jan 2018, 4:25 PM
deFault
+ 1
Generally, texts are printed horizontally, to print a text in a new line, you have to print a new line char (‘\n’), then print your text.
7th Jan 2018, 6:18 PM
Luc Hariman Randrianomenjanahary
Luc Hariman Randrianomenjanahary - avatar
+ 1
Awesome, thanks. I believe you did great
7th Jan 2018, 8:55 PM
Olagoke Tobi
Olagoke Tobi - avatar
+ 1
I'll try, but a bit later. For now, I'd like to point out, that I've changed it, just after posting it here. It was a bit incorrect, so your copy is outdated. Anyway, that code is a really badly structured mess. You shouldn't learn from it, as it's a bad example. I did it in js only because it's the only way to show anything animated here. Also, the worse is a code, the harder it is to read/understand/explain. And that one is bad.
8th Jan 2018, 8:35 PM
deFault
+ 1
Commented as requested. Hope it helps. Ask if anything specific isn't clear. PS. Set delay to 50 and enjoy the party!
8th Jan 2018, 9:36 PM
deFault
+ 1
I can't edit your codes, so I commented mine. And also, as I've said, your copy is a bit outdated. It shows for loops execution wrong. So throw it away and get a fresh copy.
9th Jan 2018, 1:32 PM
deFault
0
Thanks but I don't seem to understand the inner loop method. Here's what I could jot down from your explanation for (j=0;j<7;j++) { for (i=0; i<j; i=j) Is this what you mean??
7th Jan 2018, 4:20 PM
Olagoke Tobi
Olagoke Tobi - avatar
0
Thanks
7th Jan 2018, 4:27 PM
Olagoke Tobi
Olagoke Tobi - avatar
0
Just noticed that I've made a little error that makes the code produce output like this: ------ 0 01 012 0123 ------ which is a bit ugly, as it prints newline before the actual output, and also the height isn't correct. See if you can fix it to produce output like this: ------ 0 01 012 0123 01234 012345 ------ Hint: That would be a small change in both loops.
7th Jan 2018, 4:43 PM
deFault
0
My question is this, when printing out these codes I mean both codes, does Java print then horizontally or vertically, still trying to wrap my heads as to how these codes work.
7th Jan 2018, 6:10 PM
Olagoke Tobi
Olagoke Tobi - avatar
0
Please can you comment the working code you sent to me, I am a js guy, beginner tho. I want to understand the code. https://code.sololearn.com/WeQ3Wn11q2QO/?ref=app
8th Jan 2018, 8:22 PM
Olagoke Tobi
Olagoke Tobi - avatar
0
You commented my version or your version?
9th Jan 2018, 1:16 PM
Olagoke Tobi
Olagoke Tobi - avatar