CS106A 学习笔记(9):阶乘

第七课视频中讲的阶乘的例子:

/*
 * File:    Factorial.java
 * -----------------------------
 */

import acm.program.*;

public class Factorial extends ConsoleProgram {

    private static final int MAX_NUM = 10;

    public void run() {

        for (int i = 0; i < MAX_NUM; i++) {
            println(i + "! = " + factorial(i));
        }

    }

    private int factorial(int n) {
        int resault = 1;
        for (int i = 1; i <= n; i++) {
            resault *= i;
        }

        return resault;
    }

}

EOF

文章标签:

发表评论

5URL