java编程题1 兔子数

題古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?


package com.javaExercise;

import java.util.Scanner;

public class Exercise1 {

public static void main(String[] args) {

// TODO Auto-generated method stub

int num=2,months;

while(true){

System.out.print("请输入月数:");

Scanner i = new Scanner(System.in);

months=i.nextInt();

//TimeGo Num=new TimeGo();

System.out.println("兔子数"+(getNum(months)*2));

}

}

public static int getNum(int month){//递归

if(month == 1 || month == 2){

return 1;

}else{

return getNum(month - 1) + getNum(month - 2);

}

}

你可能感兴趣的:(java编程题1 兔子数)