编写一个java程序,在屏幕上输出1!+2!+3!…+10!的和(单层和双层for循环)

  • Post author:
  • Post category:java


package com.atrrx.java2023_3_14;

public class JiChengTest {
    public static void main(String[] args) {
        System.out.println("第一种方法:单层循环");
        int sum=0,cheng=1;
        for (int i = 1; i <=10; i++) {
            cheng*=i;
            sum+=cheng;
        }
        System.out.println(sum);
        System.out.println("*****************************");

        System.out.println("第二种方法:双层循环");
        int sum1=0,cheng1=1;
        for (int i = 1; i <=10; i++) {
            cheng1=1;
            for (int j = 1; j <= i; j++) {
                cheng1*=j;
            }
            sum1+=cheng1;
        }
        System.out.println(sum1);
//        1!+2!+3!+...+10!的阶乘和是:4037913(答案)
    }
}



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