程序分析:兔子的规律为数列1,1,2,3,5,8,13,21....即从第三个月起,本月的兔子数量为前两个月的兔子数量之和(此处单位为对)
package com.mxh.test;
public class Demo {
public static void main(String[] args) {
//上上个月兔子的数量
int lastLastMonth=1;
//上个月兔子的数量
int lastMonth=1;
//本月兔子的数量
int thisMonth=0;
//月份可自行改,根据自己情况改,1到12依次代表每个月份
for (int i=1;i<=12;i++){
if (i<=2){
System.out.println(i+"月份有"+lastMonth+"对兔子");
}else {
thisMonth=lastLastMonth+lastMonth;
//用中间变量接受
int n=lastMonth;
lastMonth=thisMonth;
lastLastMonth=n;
System.out.println(i+"月份有"+thisMonth+"对兔子");
}
}
}
}