Java递归函数 递归:方法自己调用自己 实现递归的三要素 1.方法中出现自己调用自己 2.要有分支 3.要有结束条件 //求5的阶乘 public class DiguiTest { //分析求5!的方法是什么样的方法(有参数,有返回值) //语法:访问控制修饰符<返回值类型> <方法名> ([参数列表]) public int digui(int n) { if(n==1) { return 1; }else { return n*digui(n-1); } } public static void main(String[] args) { DiguiTest t=new DiguiTest(); int s=t.digui(5); System.out.println("值为:"+s); } } 打印斐波拉契数列前20项 package test.edu.demo; //打印斐波拉契数列前20项 public class FeibolaqiTest { public int fblq(int n) { if(n==1||n==2) { return 1; }else { return (fblq(n-1)+fblq(n-2)); } } public static void main(String[] args) { FeibolaqiTest t=new FeibolaqiTest(); //int s=t.fblq(20); for(int i=1;i<20;i++) { int num=t.fblq(i); System.out.print(num+" "); } } }