用java求1! + 2! + 3! + … + 20!的结果并打印输出的内容如下图格式

怎样用java求"方法实现1!+2!+3!+…+20! 同时输出1!,2!,3!。。。。的结果!各位大侠求助~

public class test {


public static void main(String[] args) {
long s = 0;
int i = 0;
for (i = 1; i <= 20; i++) {
s += jc(i);
}
System.out.println("1!+2!+3!+…+20!的阶乘为" + s);
}
static long jc(int i){
long x = 1;
int j = 0;
for (j = 1; j <= i; j++) {
x = j * x;
}
System.out.println(i + "的阶乘为" + x);
return x;
}

}
通过测试了,绝对没有问题

public class test {
public static void main(String[] args) throws Exception {
int sum = 0;
for(int i=1;i<=20;i++){
int temp = 1;
for(int j=1;j<=i;j++){
temp*=j;
}
sum+=temp;
}
System.out.println("1-20阶乘的和为:"+sum);
}
}

答案补充:上面算法只适合于阶乘合并不是很大的情况 如果合超过int变量所能表达的值 就要用BigDecimal变量了~
例如上面程序计算1-21的阶乘和会是-927073767 导致错误
程序如下修改以后 答案就正确了53652269665821260313
非常感谢老大提醒~~~

public class T {
public static void main(String[] args) {
test2(20);
}
public static void test2(int k) {
BigDecimal total = new BigDecimal("0");
BigDecimal current = new BigDecimal("1");
for (int i = 1; i <= k; i++) {
current = current.multiply(new BigDecimal(String.valueOf(i)));
total = total.add(current);
}
System.out.println(total);
}
}

响应党的指示,完全按照党的意思,望采纳。谢谢
public class Test {
public static void main(String[] args) {
for(int i = 1; i <= 20; i++) {
long sum = 0;
long tem = 1;
for(int j = 1; j <=i; j++) {
tem = tem*j;
sum=sum +tem;
if(j < i) {
System.out.print(j + "! + ");
}else System.out.println(j + "!"+" = " + sum);
}
}
}
}
运行结果如下:
1! = 1
1! + 2! = 3
1! + 2! + 3! = 9
1! + 2! + 3! + 4! = 33
1! + 2! + 3! + 4! + 5! = 153
1! + 2! + 3! + 4! + 5! + 6! = 873
1! + 2! + 3! + 4! + 5! + 6! + 7! = 5913
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! = 46233
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! = 409113
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! = 4037913
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! = 43954713
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! = 522956313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! = 6749977113
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! = 93928268313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! = 1401602636313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! + 16! = 22324392524313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! + 16! + 17! = 378011820620313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! + 16! + 17! + 18! = 6780385526348313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! + 16! + 17! + 18! + 19! = 128425485935180313
1! + 2! + 3! + 4! + 5! + 6! + 7! + 8! + 9! + 10! + 11! + 12! + 13! + 14! + 15! + 16! + 17! + 18! + 19! + 20! = 2561327494111820313

public class Sum {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
long sum = 0;
long k = 1;
for (int j = 1; j <= i; j++) {
k = k * j;
sum = sum + k;
System.out.print(j + "!");
if (i == j) {
System.out.print("=");
} else {
System.out.print("+");
}
}
System.out.println(sum);
}
}
其中n的值可以随便改成任意整数

public static void main(String[] args) {
String info = "";
long sum_total = 0;
for (int i = 1; i <= 20; i++) {
int sum = 1;
for (int j = 1; j <= i; j++) {
sum = sum * j;
}
sum_total += sum;
info += "+ " + i + "! ";
System.out.println(info.substring(2, info.length()) + "= " + sum_total);
}
}

刚敲好。楼主看下吧。有不懂的可以再问,代码如下:
public class Test {

//计算阶乘
public static long factory(int n) {
long value = 1;
for(int i = 1; i <= n; i++) {
value *= i;
}
return value;
}
public static void main(String[] args) {
long sum = 0;
for(int i = 1; i <= 9; i++) {

//计算累加
sum += factory(i);

//输出格式
for(int j = 1; j <= i; j++) {
System.out.print(j + "!");
if(j != i) {
System.out.print(" + ");
}
}
System.out.println(" = "+sum);
}
}
}

总结:楼上楼下的都是大侠,我在这里学习了

相关兴趣推荐

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网