Java写一个菱形和一个空心菱形
时间: 2023-03-28 08:04:17 浏览: 86
用Java写的空心菱形
好的,我可以回答这个问题。以下是 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 = ; i < n; i++) {
for (int j = ; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = ; j < 2 * i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = n - 2; i >= ; i--) {
for (int j = ; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = ; j < 2 * i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
```
空心菱形:
```
import java.util.Scanner;
public class HollowDiamond {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入菱形的边长:");
int n = scanner.nextInt();
for (int i = ; i < n; i++) {
for (int j = ; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = ; j < 2 * i + 1; j++) {
if (j == || j == 2 * i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int i = n - 2; i >= ; i--) {
for (int j = ; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = ; j < 2 * i + 1; j++) {
if (j == || j == 2 * i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
```
希望这个回答能够帮到你!
阅读全文