首页 > 代码库 > java面试每日一题11

java面试每日一题11

题目:求1+2!+3!+...+20!的和

public class Recursion {
    public static void main(String args[]) throws NumberFormatException, IOException{
        System.out.println("请输入一个数");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int num1=Integer.parseInt(br.readLine());
            int value=http://www.mamicode.com/getRecursion(num1);
            System.out.println(num1+"的阶乘是"+value);
            int result1=getAdd(num1);
            System.out.println("结果是"+result1);
    }

    public static int getRecursion(int num){
        if(num==1){
            return 1;
        }
        else{
            return num*getRecursion((num-1));
        }
    }
    public static int getAdd(int res){
        int result=0;
        for(int i=1;i<=res;i++){
            result+=getRecursion(i);
        }
        return result;
    }
}

 

java面试每日一题11