java代码打印一段菱形
时间: 2023-12-02 10:04:23 浏览: 68
Sure, here's one way to print a diamond pattern with Java code:
```
public static void printDiamond(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = n-1; i >= 1; i--) {
for (int j = 1; j <= n-i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
```
To use this code, you would call `printDiamond(n)` where `n` is the number of rows in the diamond (must be odd). The result will be printed to the console.
阅读全文