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
15 odpowiedzi
+ 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.
+ 4
// With one loop
int height = 5, i = 0;
String s = "";
do
System.out.println(s += i);
while (i++ < height);
+ 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.
+ 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");
}
+ 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.
+ 1
Awesome, thanks. I believe you did great
+ 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.
+ 1
Commented as requested. Hope it helps. Ask if anything specific isn't clear.
PS. Set delay to 50 and enjoy the party!
+ 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.
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??
0
Thanks
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.
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.
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
0
You commented my version or your version?