Home > Back-end >  How is playing the graphic below?
How is playing the graphic below?

Time:04-05

Can understand why there are six rows, just can't understand why only a * in the first row, second row has two *, the third row has three *?
 for (int I=1; i<7. I++) {
for(int j=0; J<=I - 1; J++) {
System.out.print("*");
}
System.out.println();
}
}

*
* *
* * *
* * * *
* * * * *
* * * * * *

CodePudding user response:

This is two loops, outer loop controls the rows for a total of 6, inner loop controls the number of each row; When the outer loop controls the I=1 to print the first row, inner loop, when I=1, j<=0, for loop loop only once, print a star, when I=2, outer loop control to print the second row, inner loop j< at this moment;=1, j=0, start cycle, print two times, two stars, and so on,

CodePudding user response:

reference 1st floor sh_c_2450957609 response:
this is two loops, external loop controlling the rows for a total of 6, inner loop controls the number of each row; When the outer loop controls the I=1 to print the first row, inner loop, when I=1, j<=0, for loop loop only once, print a star, when I=2, outer loop control to print the second row, inner loop j< at this moment;=1 to j=0, start cycle, print two times, two stars, and so on,


Why j=0

CodePudding user response:

Cycle did not perform a externally for loop code will re-execute side, at this moment (int j=0; J<=1; J++)!

CodePudding user response:

Outer loop every execution

CodePudding user response:

refer to the second floor recentlywe response:
Quote: refer to 1st floor sh_c_2450957609 response:
this is two loops, external loop controlling the rows for a total of 6, inner loop controls the number of each row; When the outer loop controls the I=1 to print the first row, inner loop, when I=1, j<=0, for loop loop only once, print a star, when I=2, outer loop control to print the second row, inner loop j< at this moment;=1 to j=0, start cycle, print two times, two stars, and so on,


Why j=0

Outer loop for each execution, he {} in the scope of the code to execute one side, when I=2, j<=I - 1 is j<=1, then the conditions of the inner loop is
for(int j=0; J<=1; J++) {}, from one side of the j=0 began to perform for the inner loop, then print the two stars, the for loop {}
The following println () output is just nothing for the newline
When I=3, the for (int j=0; J<=2; J++) loop to print three star, then the following line, and so on
  • Related