java

导航

java认证考试编程题(18)

来源 :中华考试网 2016-10-06

写一个方法,输入任意一个整数,返回它的阶乘

/**

     * 获得任意一个整数的阶乘

     * @param n

     * @return n!

     */

    public int factorial(int n)

    {

       //递归

       if(n==1)

       {

           return 1;

       }

       return n*factorial(n-1);

       //非递归                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

//     int multi = 1;

//     for (int i = 2; i <= n; i++) {

//         multi*=i;

//     }

//     return multi;

    }

分享到

相关资讯