Java杨辉三角,不使用数组

Java学习笔记

今天在用Java写杨辉三角的时候,想着用不要数组的方式,只用循环的变量去控制。想了一个下午没有写出来,原因仅仅是两句语句的顺序反了而已。
这个问题,在JumperMan的博客里面找到了答案。

不使用数组,仅用循环控制

public static void main(String[] args) {
        // 行数
        int iLines = 5;
        // 乘数
        int iMul;
        // 杨辉三角公式
        for (int i = 0; i < iLines; i++) {
            iMul = 1;
            for (int j = 0; j <= i; j++) {
                //这行代码和下面的代码互换,就会打印错误的杨辉三角
                System.out.print(iMul + "\t");
                //1111
                iMul = iMul * (i - j) / (j + 1);
            }
            System.out.println();
        }
    }

[ 百度百科:杨辉三角]

猜你喜欢

转载自blog.csdn.net/m0_37068073/article/details/81459537