在键盘上输入数n,编程计算sum=1!+2!+··· + n!的结果

import java.util.Scanner;

/**

 * 求出1到n的阶乘之和

 */

public class E21Jiecheng {

   public static void main(String[] args)

   {	   

	   System.out.print("请输入求阶乘的数字n:");

	   	   

	   Scanner reader=new Scanner(System.in);

	   int n=reader.nextInt();

	   int sum=0;

	   int s=1;	   

	   for(int i=1;i<=n;i++)

	   {

		  s=s*i;

		  sum=sum+s; 

	   }

	   System.out.println("1到"+n+"的阶乘之和为:" +sum);	      

   }

}

我的:

package n;

import java.util.Scanner;

public class n {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int n=0,i=0,sum=1,j=0,N=0;//计201 赵世伟
        Scanner in=new Scanner(System.in);
        System.out.println("请输入n:");
        n=in.nextInt();
        for (i=1;i<=n;i++){
            sum=1;
            for (j=1;j<=i;j++){
                sum=sum*j;
            }
            N=N+sum;
        }
        System.out.println("1!+2!+3!+...+"+n+"!"+"="+N);  
    }
}

 

你可能感兴趣的:(java基础,java)