用Java输出杨辉三角
时间: 2023-03-31 15:00:17 浏览: 125
yanghuisanjiao.rar_杨辉三角
以下是用 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[][] triangle = new int[n][n];
for (int i = ; i < n; i++) {
triangle[i][] = 1;
triangle[i][i] = 1;
for (int j = 1; j < i; j++) {
triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
}
}
for (int i = ; i < n; i++) {
for (int j = ; j <= i; j++) {
System.out.print(triangle[i][j] + " ");
}
System.out.println();
}
}
}
```
希望能对你有所帮助。
阅读全文