用java写输出一个菱形
时间: 2023-03-27 19:05:00 浏览: 99
您好,我是 C 知道。以下是用 Java 写输出一个菱形的代码:
```
import java.util.Scanner;
public class DiamondPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入菱形的行数:");
int rows = sc.nextInt();
int i, j, space = 1;
space = rows - 1;
for (j = 1; j <= rows; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j <= rows - 1; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}
space++;
for (i = 1; i <= 2 * (rows - j) - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
}
}
```
希望对您有所帮助。
阅读全文