Java实现n的阶乘计算分别用递归和非递归

    /**
     * Java实现n的阶乘分别用递归和非递归
     *
     * @author hjsjy
     * @create 2018/9/30
     * @since 1.0.0
     */
    public class factorialtest {

        /**
         * 递归方法
         * @param a
         * @return
         */
         public static int testA(int n){

             if(n==0) {
                 return 1;
             }else{
                 int b=1;
                 while(n>0){
                     b=b*n;
                     n=n-1;
                 }
                 return b;
             }
         }

        /**
         * 非递归的方法
         * @param b
         * @return
         */
         public static int testB(int n){

            if(n==0){//终止条件
                return 1;
            }
            return n*testB(n-1);//递归
        }

    }

猜你喜欢

转载自blog.csdn.net/hj1997a/article/details/82913792