我有一個困難時期試圖瞭解背後產生這樣的輸出嵌套循環程序邏輯:邏輯
Pattern C
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
而這裏的代碼:
void patternC(){
System.out.println("\nPattern C");
for(int m = 1; m <= a; m++){ //<-- a is for the desired number of lines
//spasi
for(int n = m; n <= a-m+a ; n++){
System.out.print(" ");
}
for(int o = 1; o <= m ; o++){
System.out.print(o + " ");
}
System.out.println();
}
}
我知道第一個for
用於該行,而第三個for
用於在每行中打印的數字。但我仍然沒有在第二個for
(我知道這是間距)的邏輯,但你可以請示例解釋我?謝謝。
明白嘗試打印出'm'' n'和'o'的值而不是 –