java输出一个菱形
时间: 2024-02-24 07:55:08 浏览: 59
以下是Java代码,可以输出一个菱形:
```
import java.util.Scanner;
public class Diamond {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入菱形的行数:");
int n = input.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++) {
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++) {
System.out.print("*");
}
System.out.println();
}
}
}
```
运行程序后,输入菱形的行数,即可输出一个菱形。
阅读全文