java输出一个实心的菱形
时间: 2024-05-15 13:16:04 浏览: 83
以下是Java代码,可以输出实心的菱形:
```
import java.util.Scanner;
public class Diamond {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入菱形的边长:");
int n = scanner.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++) {
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++) {
System.out.print("*");
}
System.out.println();
}
}
}
```
运行程序后,输入菱形的边长,程序就会输出一个实心的菱形。
例如,输入6,程序输出的结果如下:
```
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
```
阅读全文