Java输出空间菱形
时间: 2024-02-17 12:02:02 浏览: 61
以下是一个 Java 程序,可以输出空心的菱形:
```java
import java.util.Scanner;
public class PrintDiamond {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入菱形的大小:");
int size = sc.nextInt();
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= size - 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 = size - 1; i >= 1; i--) {
for (int j = 1; j <= size - 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();
}
}
}
```
运行程序后输入菱形的大小,即可输出指定大小的菱形。输出的菱形中,空心部分用空格填充。
阅读全文