java等腰杨辉三角
时间: 2023-11-12 11:56:04 浏览: 82
java杨辉等腰三角形
好的,我可以帮你编写一个Java程序来生成等腰杨辉三角。以下是示例代码:
```java
import java.util.Scanner;
public class PascalTriangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要生成的行数:");
int numRows = scanner.nextInt();
int[][] triangle = new int[numRows][numRows];
for (int i = 0; i < numRows; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
triangle[i][j] = 1;
} else {
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
}
}
}
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numRows-i-1; j++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print(triangle[i][j] + " ");
}
System.out.println();
}
}
}
```
运行以上代码,您可以输入要生成的行数,然后程序将输出相应行数的等腰杨辉三角。
阅读全文