java输入一个空心菱形
时间: 2023-10-17 12:13:35 浏览: 32
Java 的空心菱形
下面是 Java 代码实现一个空心菱形的图案:
```java
import java.util.Scanner;
public class HollowDiamondPattern {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入菱形的边长:");
int n = input.nextInt();
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++) {
if (j == 1 || j == 2 * i - 1) {
System.out.print("*");
} else {
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++) {
if (j == 1 || j == 2 * i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
输入菱形的边长后,程序会输出一个空心的菱形图案。
阅读全文