package TestSuanFa; import java.util.Scanner; /** * 兔子產仔 * * @author Joy * */ public class TuziShengZai { // 參數n爲時間,至關於數列的第幾位值 public static int Fib(int n) { if (n == 1 || n == 2) { //1,2月值得返回值都相同 return 1; } int num = Fib(n - 1) + Fib(n - 2); // 從數列3後面開始,每一個數列值都是前面兩項值 return num; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("兔子產仔問題求解!"); System.out.print("請輸入時間:"); int n = input.nextInt(); int Num = Fib(n); System.out.println(n + "個月後,共能繁殖" + Num + "個兔子"); } }