二维数组小练习 打印 杨辉三角(优化)

`
public class YanghuiTest2 {
public static void main(String[] args) {

    //动态初始化一个10行的二维数组
    int[][] yh = new int[10][];
    for (int i = 0; i < yh.length; i++) {
        yh[i] = new int[i+1];
        for (int j = 0; j <= i; j++) {
            if (j == 0 || j == i) {
                yh[i][j] = 1;
            }else {
                yh[i][j] = yh[i-1][j-1] + yh[i-1][j];
            }

            System.out.print(yh[i][j] + "\t");
            if (i == j) {
                System.out.println();
            }
        }
    }
}

}
`

posted @ 2020-12-24 15:50  dog_IT  阅读(88)  评论(0)    收藏  举报