斐波那契数列算法
package chapter;
import java.util.*;
public class Demo1 {
public static void main(String[] args) {
//假定所求的月份为整型10
Scanner input = new Scanner(System.in);
System.out.println("请输入你所要求的月份");
int n = input.nextInt();
//标准的System类输出
System.out.println("第" + n + "个月兔子总数为" + fun(n));
}
//定义私有类的方法,采用递归调用的方法
private static int fun(int n) {
//当所求月份为1或2月时,返回值为1
if(n == 1||n == 2)
return 1;
//递归调用
else
return fun(n - 1) + fun(n - 2);
}
}
``