java 输出一个空心菱形
时间: 2023-11-20 16:07:17 浏览: 66
以下是 Java 代码实现空心菱形的输出:
```java
import java.util.Scanner;
public class Diamond {
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 k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 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 k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 2 * i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
输入菱形的边长后,程序会输出一个空心的菱形图形。
阅读全文