兔子生兔子
/**
*
* 有一对兔子,从出生后第3个月起每个月都生一对兔子,
* 小兔子长到第三个月后每个月又生一对兔子,
* 假如兔子都不死,问每个月的兔子对数为多少?
* @author zengxin
*
*/
public class page01兔子生兔子问题 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.println("请输入月份:");
int month = sc.nextInt();
for(int i=1;i<=month;i++){
System.out.println(i+"月份的兔子总数为:"+getRabbit(i));
}
} catch (Exception e) {
}finally {
if(sc!=null){
sc.close();
}
}
}
private static int getRabbit(int n){
if(n==1 || n==2){
return 1;
}else{
return getRabbit(n-1) + getRabbit(n-2);
}
}
}
