一个简单的嵌套循环,我就不用面向对象的思想写了,给个主体的代码你:
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);
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);
}
}
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);} }
这是个求累加和的!书上有!