java%E8%BE%93%E5%87%BA%E4%B8%80%E4%B8%AA%E7%A9%BA%E5%BF%83%E8%8F%B1%E5%BD%A2
时间: 2023-08-31 21:06:43 浏览: 149
韩顺平_JAVA_视频教程_下载
5星 · 资源好评率100%
要在Java中输出一个空心菱形,可以使用嵌套循环来实现。下面是一个示例代码:
```java
import java.util.Scanner;
public class Main {
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 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();
}
}
}
```
这段代码会要求你输入菱形的行数,然后根据输入的行数打印出相应的空心菱形。
阅读全文