java打印菱形图案
时间: 2023-08-22 08:05:05 浏览: 110
以下是Java打印菱形图案的代码:
```java
import java.util.Scanner;
public class DiamondPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows : ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = n; j > i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}
System.out.println();
}
sc.close();
}
}
```
运行程序后,将提示输入行数。输入后,程序将打印出相应的菱形图案。
阅读全文