java输出一个杨辉三角
时间: 2023-04-08 11:04:45 浏览: 117
以下是Java代码,可以输出一个杨辉三角:
```
import java.util.Scanner;
public class YangHuiTriangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入杨辉三角的行数:");
int n = scanner.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();
}
}
}
```
阅读全文