用JAVA写杨辉三角的读取
时间: 2023-09-28 18:13:39 浏览: 66
下面是用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++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
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();
}
}
}
```
运行该程序后,会提示用户输入要打印的行数,然后输出相应的杨辉三角。
阅读全文