用Java语言两个for打印出菱形
时间: 2023-11-19 14:06:32 浏览: 67
以下是用Java语言打印菱形的示例代码:
```java
public class PrintDiamond {
public static void main(String[] args) {
int n = 5; // 菱形的行数
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
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 k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
```
输出:
```
*
***
*****
*******
*********
*******
*****
***
*
```
阅读全文