使用二维数组打印任意行的杨辉三角
package com.java.arraysdemo;
import java.util.Scanner;
public class ArrayDemo3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入杨辉三角的行数:");
int number = scanner.nextInt();
int [][] pascalTriangle = new int[number][];
for (int i = 0; i < pascalTriangle.length; i++) {
pascalTriangle[i] = new int[i + 1];
pascalTriangle[i][0] = 1;
pascalTriangle[i][i] = 1;
if(i > 1){
for (int j = 1; j < pascalTriangle[i].length -1; j++) {
pascalTriangle[i][j] = pascalTriangle[i-1][j-1] + pascalTriangle[i-1][j];
}
}
}
for (int i = 0; i < pascalTriangle.length; i++) {
for (int j = 0; j < pascalTriangle[i].length; j++) {
System.out.print(pascalTriangle[i][j]+"\t");
}
System.out.println();
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9910092978db2266ea0199d8f025306f.png#pic_center)