递归

A方法调用A方法。       栈机制

前提:基数小,不然运行不动

 

两个部分:  递归头:表示 什么时候不调用自己。否则陷入了死循环。

                   递归体:什么时候需要调用自身方法。

package 狂神;

public class 递归 {

public static void main(String[] args) {

System.out.println(f(5));
}
public static int f(int n){              //递归体 
if(n==1){        
return 1;
}else{                        //递归头
return n*f(n-1);          // 自己调用自己
}
}
}

posted @ 2022-03-15 20:04  小布丁dd  阅读(20)  评论(0)    收藏  举报