public static void main(String[] args) {//菱形實心時,數量為1,2,3...... int a = 10;//菱形的邊為10個 * for (int i = 1; i <= a; i++) { for (int j = 1; j <= a - i; j++) ...
public static void main(String[] args) {//菱形實心時,數量為1,2,3......
int a = 10;//菱形的邊為10個 *
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= a - i; j++) {
System.out.print(" ");//列印空格
}
for (int k = 1; k <= i; k++) {
if (k == 1 || k == i) {//只有第一個和最後一個位置輸出 * ,其他位置輸出空格
System.out.print("* ");
} else {
System.out.print(" ");// 2倍的空格
}
}
System.out.println();
}//列印上半部分菱形(即三角形)
for (int i = 1; i < a; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int m = 1; m <= a - i; m++) {
if (m == 1 || m == a - i) {
System.out.print("* ");
} else {
System.out.print(" ");//2倍的空格
}
}
System.out.println();
}
}
}