java 编程:计算1!+2!+3!+…..+10!,其中阶乘的计算用方法实现。

谢谢^_^

import org.junit.Test;

public class Factorial {
@Test
public void qiuhe() {

//下面三行可以根据不同需求改造成自己需要的,然后计算。
String str = "1!+2!+3!+4!+5!+6!+7!+8!+9!+10!";
str=str.replace("+","");
String[] array=str.split("!");

long sum= 0;
for (int i = 0; i <array.length ; i++) {
sum += factorial(Integer.parseInt(array[i]));
}
System.out.println(sum);
}

// factorial 英文翻译 阶乘
public long factorial(int number) {
long result = 1;
for (int i = 2; i <= number; i++) {
result *= i;
}
System.out.println(number+"的阶乘是:"+result);
return result;
}

}
温馨提示:答案为网友推荐,仅供参考