编写一个java程序,从键盘上输入一个n,计算1的阶乘加到n的阶乘

  • Post author:
  • Post category:java


public class Test5 {
    public static void main(String[] args) {
        int n=0,i=0,sum=1,j=0,N=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入n");
        n=sc.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);
    }
}
请输入n
5
1!+2!+3!+...+5!=153

Process finished with exit code 0



版权声明:本文为weixin_52486560原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。