java

当前位置:中华考试网 >> java >> java面试题 >> 文章内容

java认证考试编程题(18)

来源:中华考试网  [2016年10月6日]  【

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

/**

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

     * @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;

    }

责编:sunny
  • 会计考试
  • 建筑工程
  • 职业资格
  • 医药考试
  • 外语考试
  • 学历考试