for循环实现3的阶乘:3*2*1
public class Demo { public static void main(String[] args) { int For_JieCheng=1; for(int i=1; i<=3; i++){ For_JieCheng*=i;//For_JieCheng=For_JieCheng*i } System.out.println(For_JieCheng); } }
递归方法实现
public class Demo { public static int Digui_JieCheng(int i){ if(i==1){return i;} else{ return i*Digui_JieCheng(i-1);}//① } public static void main(String[] args) { System.out.println(Digui_JieCheng(3)); } } // ①递归的核心:方法自己调用自己 //做递归要写一个方法,一般if是递归的出口,else是规律
递归方法实现原理