刚开始学,今天做了一个题,小小成就感和大家分享一下,求大神指导!
代码加了截图,便于大家看得更清楚直观一点儿~
/**
* 计算1!+2!+...+10!的值!
*/
public class FactorialDemo {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += getFactorial(i);
}
System.out.println(sum);
}
// 计算某数阶乘的方法
public static int getFactorial(int x) {
if (x == 0)
return 0;
else {
int factorial = 1;
for (int i = 1; i <= x; i++) {
factorial *= i;
}
return factorial;
}
}
}
|
|