/** * 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);//遞歸 } }