编写程序计算1!+2!+3!+…+n!,并输出计算结果。

 

 1 import java.util.Scanner;
 2 
 3 public class Factorial {
 4     public static void main(String[] args) {
 5         long t = 1;
 6         long sum = 0;
 7         Scanner sc = new Scanner(System.in);
 8         if(sc.hasNextInt()){        ////判断输入值是否为int类型
 9             int n = sc.nextInt();
10             for (int i = 1; i <= n; i++) {
11                 for (int j = 1; j <= i; j++) {      //计算n的阶乘
12                     t *= j;
13                 }
14                 sum += t;
15                 t = 1;
16             }
17             System.out.println("小于整数:"+n+"的所有正整数阶乘之和是:"+sum);
18         }
19         else
20             System.out.println("输入错误!");
21     }
22 }

 

posted on 2019-11-27 11:30  墨问浮生  阅读(205)  评论(0)    收藏  举报