用java语言打印一个空心菱形
时间: 2023-05-29 21:02:43 浏览: 107
以下是Java代码:
```
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入菱形边长:");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
System.out.print("*");
if (i > 1) {
for (int k = 1; k <= 2 * i - 3; k++) {
System.out.print(" ");
}
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(" ");
}
System.out.print("*");
if (i > 1) {
for (int k = 1; k <= 2 * i - 3; k++) {
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
}
}
```
运行后输出对应边长为5的空心菱形:
```
请输入菱形边长:
5
*
* *
* *
* *
* *
* *
* *
* *
*
```
阅读全文