Write a Java program to find the sum of 1~n!

1. Topic requirements:

Find the factorial sum of 1~n numbers, where n is the number entered by the keyboard.

Second, the knowledge that needs to be mastered:

1. Scanner keyboard input technology

2. Method definition and call

3. Implementation method

Define a method, use a loop in the method, and use a loop to call repeatedly when calling

Fourth, implement the code

public class TestJiecheng { 
    public static void main(String[] args) { 
        Scanner scanner = new Scanner(System.in); 
        System.out.println("Please enter an integer: "); 
        int n = scanner.nextInt(); 
        System.out.println("The factorial sum of this integer is: " + circulation(n)); 

    } 

    //Circulation method 
    public static int circulation(int n) { 
        int sum = 0; 
        int num = 1; 
        for (int i = 1; i <= n; i++) { 
             num*= i; 
             sum+=num; 
        } 
        return sum; 
    } 
}

5. Output result

ps: I am a novice, a beginner in Java, and I am still immature. If there is anything wrong, I would like to ask the masters from all walks of life to give me advice and give me some advice, and I will listen to it when I make up my mind!

Guess you like

Origin blog.csdn.net/m0_72237363/article/details/130160356