编写一个java应用程序求1!+2!+...+20!的和

2024-12-23 14:38:04
推荐回答(4个)
回答1:

一个简单的嵌套循环,我就不用面向对象的思想写了,给个主体的代码你:
long sum = 0;
for(int i = 1;i <= 20; i++){
long mul = 1l;
for(long j = 1L;j <= i;j++){
mul *= j;
}
sum += mul;
}
System.out.println("1!+2!+...+20!的和:"+sum);

回答2:

public class testleijia
{
public static void main(String args[])
{
long sum =0l;

for (int i = 1; i <= 20; i++)
{
long ji = 1;
for (int j = 1; j <=i; j++)
{
ji *= j;
}

sum += ji;
}

System.out.println("sum is:" + sum);
}
}

回答3:

public class test
{
public static void mian(String args[])
{int k,i,j,sum,a;
sum=0;
a=1;
k=20;
for (i=1;i<=k;i++)
{a=1;
for(j=1;j<=i;j++)
a=a*j;
sum+=a;}
System.out.println(sum);} }

回答4:

这是个求累加和的!书上有!