java输出杨辉三角
时间: 2023-12-23 14:24:06 浏览: 68
好的,以下是 Java 输出杨辉三角的代码:
```
import java.util.Scanner;
public class YangHuiTriangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入杨辉三角的行数:");
int n = input.nextInt();
int[][] arr = new int[n][n];
for (int i = 0; i < n; i++) {
arr[i][0] = 1;
arr[i][i] = 1;
}
for (int i = 2; i < n; i++) {
for (int j = 1; j < i; j++) {
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(arr[i][j] + " "); }
System.out.println();
}
}
}
```
阅读全文