So my solution was this:
int m = 10;
for(int i = 0; i <= m; i++) {
for(int j = 0; j < i; j++) {
System.out.print(8);
}
System.out.println();
}
which prints exactly like the requested output, but won't work so I copied this solution:
for(int i = 0; i < 10; i++)
{
for(int j = 0; j <= i; j++)
{
System.out.print(8);
}
System.out.print("\n");
}
which does exactly the same thing and prints the same output, but this one works!
I really want to know why? Because I used the same things, 2 for loops. Thanks.
Why is it not ok like this?
Resolved
Comments (6)
- Popular
- New
- Old
You must be signed in to leave a comment
raulGLD
21 October 2020, 09:03
So I checked again I figure it out, the output was perfectly fine, it was just that my original version simply prints out an extra new line at the beginning of the triangle. If I change first loop to "<" and second one to "<=" I get the same answer except the first new line. So to sum everything up, the problem was the first new line because at the first iteration of the first for loop the 2nd loop didn't run because i was equal to j and not smaller. Thank you everyone for helping me clear it out. Have a great day!
0
Jiří Ćmiel
21 October 2020, 07:59
If you use <= you should start counting from 1 not from 0. You are either including last count or not.
int j = 0; j <= 10 leads to count 11. Count on fingers
int j = 0; j < 10 leads to count 10.
0
raulGLD
21 October 2020, 05:15
Hey Augustin, thanks for replying, I think you meant in the second for loop to do
but even then, I still get eleven 8s instead of ten 8s. And the example output and my output also, has ten lines of 8s. So I still think my code is right or I still can't figure out what I did wrong. 0
Agustin Jauregui
20 October 2020, 15:26
This may help you spot what's wrong. It's your first code but adding "=" in the second loop.
0
raulGLD
20 October 2020, 06:55
Hey Vincenzo, I have counted 3 times in my IDE, it's 10 lines of 8, starting with one 8, and ending in ten 8s. I am missing something here? ![]()

0
Vincenzo Seggio
19 October 2020, 10:16solution
Hi, in your solution you print 11 lines with 8. Should be 10 lines
+2