美文网首页基础编程50题
【习题22】递归求阶乘

【习题22】递归求阶乘

作者: Xplorist | 来源:发表于2017-03-26 18:37 被阅读16次

    【程序22】
    题目:利用递归方法求5!。

    package com.share.test21_30;
    
    /**
     * 【程序22】题目:<br>
     * 利用递归方法求5!。 
     * 
     * @author brx
     */
    public class Test22 {
        public static void main(String[] args) {
            System.out.println(test(5));
        }
        /**
         * 思路:<br>
         * 每一层的数乘以下一层的数,再一层一层的找回来,最后就将所有的数相乘的结果返回来了
         * @param n:对n求阶乘
         * @return 返回一个n的阶乘的结果
         */
        public static int test(int n){
            int result=1;
            if(n>1){
                result=n*test(n-1);
            }
            return result;
        }
    }
    
    

    相关文章

      网友评论

        本文标题:【习题22】递归求阶乘

        本文链接:https://www.haomeiwen.com/subject/homkottx.html