使用java语言打印出一个空菱形图案
时间: 2023-09-18 22:08:21 浏览: 92
以下是使用Java语言打印出一个空菱形图案的代码:
```java
import java.util.Scanner;
public class DiamondPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入菱形的行数:");
int rows = sc.nextInt();
int space = rows - 1;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= space; j++) {
System.out.print(" ");
}
space--;
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();
}
space = 1;
for (int i = 1; i <= rows - 1; i++) {
for (int j = 1; j <= space; j++) {
System.out.print(" ");
}
space++;
for (int j = 1; j <= 2 * (rows - i) - 1; j++) {
if (j == 1 || j == 2 * (rows - i) - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
你可以运行这段代码,输入菱形的行数,然后它将打印出相应的空菱形图案。
阅读全文