java 实现杨辉三角
时间: 2024-04-29 20:24:17 浏览: 81
```
import java.util.Scanner;
public class YanghuiTriangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入要打印的行数:");
int n = sc.nextInt();
int[][] arr = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || i == j) {
arr[i][j] = 1;
} else {
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
```
运行结果:
请输入要打印的行数:6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
阅读全文