Java-01,计算1-10的阶乘之和
Posted 润青
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 f = %d ",i,f); result += f; System.out.printf("result = %d ",result); } System.out.printf("result = %d ",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的阶乘之和的主要内容,如果未能解决你的问题,请参考以下文章