# 递归的简单阐述

``````    (if (<=  x 1)    //1.必须有一个输出口
1                  //出口值
(* x (factoral (- x 1)))
``````

``````     *
* @param n          计算5！ 则n =5
* @param product    一般是设置为1  比如 计算5! factail1(5,1)
* @return           返回递归结果
*/
public static long facttail1(int n, int product)
{
if(n<0)
{
return 0;
}
if(1 == n)
``````

{

``````   return product;
``````

} else {

``````    return  facttail1(n-1, product*n);
``````

}

``````}
``````

``````     *
* @param n   5! 则 n = 5
* @return    返回阶乘的计算结果
*/
public static long facttail1(int n)
{
long a = 0;// 用于调试，可注释
if(n==1)
{
a = 1;// 用于调试，可注释
return 1;
}
a =n*facttail1(n-1);  // 用于调试，可注释
return n*facttail1(n-1);
}
``````

``````     *
* @param n   计算 5！ 则n=5
* @return
*/
public static int fact2(int n)
{
``````

if(n < 0) {

``````  throw new IllegalArgumentException();
``````

} int result = 1; for(int i = 1; i <= n; i++) {

`````` result = result*i;
``````

}

`````` return result;
}
``````

``````    {
// TODO 自动生成的方法存根
long Apple =facttail(5,1);
System.out.println("Apple get by 尾递归："+Apple);
long Banana =facttail1(5);
System.out.println("Banana get by 递归："+Banana);
}
``````

Apple get by 尾递归：93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 Banana get by 递归：93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000