我有這樣的代碼,我試圖打印以下形狀了......倒車的Java For循環
****
* *
**
*
目前代碼:
System.out.print("\nEnter number of rows: ");
rows = kybd.nextInt();
for (int i = 0; i < rows; i++) {
System.out.print("*");
}
for (int i = 0; i < rows; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == rows - 1 || j == i) {
System.out.print("*");
} else {
System.out.print("0");
}
}
System.out.println();
}
出於某種原因,它打印出像這個:
****
*
**
* *
任何想法如何扭轉第二個循環,所以它打印另一種方式?
_For某種原因_...你要我們找到原因,因爲你不知道如何調試? –
'System.out.print(「0」)如何進入? – NPE
你想'for(int i = rows - 1; i> 0; i - )'。你想在你進一步深入之前去閱讀所有關於'for'循環的內容。 –