码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-02-21 21:59:50      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:i++   system   stat   执行   col   ring   world   style   计算   

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

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

标签:i++   system   stat   执行   col   ring   world   style   计算   

原文地址:https://www.cnblogs.com/nyist0/p/12343095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!