用java打印空心菱形
时间: 2024-03-09 18:20:35 浏览: 78
java 打印空心菱形
5星 · 资源好评率100%
以下是用Java打印空心菱形的代码:
```java
import java.util.Scanner;
public class HollowDiamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
for (int k = 1; k <= i * 2 - 1; k++) {
if (k == 1 || k == i * 2 - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i * 2 - 1; k++) {
if (k == 1 || k == i * 2 - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
运行结果:
```
Enter the number of rows: 5
*
* *
* *
* *
* *
* *
* *
* *
*
```
阅读全文