用Java输出一个棱形
时间: 2023-05-16 09:05:42 浏览: 60
java程序输出菱形
以下是用 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("");
}
}
}
```
输入行数为 5,输出结果如下:
```
*
***
*****
*******
*********
*******
*****
***
*
```
希望能够帮到您!
阅读全文