Java代码实现空心菱形
时间: 2023-08-31 10:12:44 浏览: 93
实验——使用for循环输出空心菱形-java流程控制
以下是Java代码实现空心菱形的示例:
```
import java.util.Scanner;
public class HollowRhombus {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入菱形的边长:");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 2 * i - 1) {
System.out.print("*");
} else {
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 k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 2 * i - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
输入菱形的边长后,程序会输出一个空心的菱形,其内部为空,外部由*符号组成。
阅读全文