Java-01,计算1-10的阶乘之和

public class HelloWorld{
    public static void main(String[] args) {
        long result = 0;
        long f = 1;
        for(int i = 1; i <= 10; i++){
            f = f * i;
            System.out.printf("i = %d\n f = %d\n",i,f);
            result += f;
            System.out.printf("result = %d\n",result);
        }
        System.out.printf("result = %d\n",result);
    }
}

执行结果:

i = 1
f = 1
result = 1
i = 2
f = 2
result = 3
i = 3
f = 6
result = 9
i = 4
f = 24
result = 33
i = 5
f = 120
result = 153
i = 6
f = 720
result = 873
i = 7
f = 5040
result = 5913
i = 8
f = 40320
result = 46233
i = 9
f = 362880
result = 409113
i = 10
f = 3628800
result = 4037913
result = 4037913

posted @ 2020-02-21 21:02  润青  阅读(2736)  评论(0编辑  收藏  举报