标签:sys rgs java out bsp stat ring ati factorial
public static void main(String[] args) {
factorial(); //調用求階乘求和的方法
}
public static void factorial() {
long factorial = 1; // 定義初始值為1
long sum = 0; // 階乘和
for (int i = 1; i <= 20; i++) {
factorial *= i; //等價於factorial = factorial * i; 例如:黨i = 3 時,factorial =1*2*3;
sum += factorial; // i 每循環一次sum就加上前一個factorial的值
}
System.out.println(sum);
}
标签:sys rgs java out bsp stat ring ati factorial
原文地址:https://www.cnblogs.com/merrysr/p/13052919.html