上圖爲分析圖解(初學者的一點心得)
public static void main(String[] args) {
// rows(菱形總行數)一定是奇數
int rows = 17;
// row_s菱形上半部分
int row_s = rows / 2 + 1;
// row_x菱形下半部分
int row_x = rows / 2;
// 先打印上半部分
/**
* !!!!!!當前行數(i)從0開始!!!!!!
*/
for (int i = 0; i < row_s; i++) {
// 打印倒得直角三角形(每行減一個,與當前行數(i)和總行數(row_s)的關係:row_s-i-1)
for (int j = 0; j < row_s - i - 1; j++) {
System.out.print(" ");
}
// 打印正的等腰三角形(每行加兩個,與當前行數(i)的關係:2*i+1)
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print("*");
}
// 每行換行
System.out.println();
}
// 打印下半部分
for (int i = 0; i < row_x; i++) {
// 打印正的直角三角形(每行加一個,與當前行數(i)的關係:i+1)
for (int j = 0; j < i + 1; j++) {
System.out.print(" ");
}
// 打印倒得等腰三角形(每行減2個,與當前行數(i)和總行數(row_x)關係:2*row_x-2*i-1)
for (int j = 0; j < 2 * row_x - 2 * i - 1; j++) {
System.out.print("*");
}
// 每行換行
System.out.println();
}
}
</pre><pre>