急,求大神指教。
有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?该怎么算?
import java.util.*;
class TuZi {
public int age = 0;
public TuZi produce() {
TuZi t = new TuZi();
return t;
}
}
public class produce {
public static void main(String args[]) {
ArrayList<TuZi> tz = new ArrayList<TuZi>();
TuZi t = new TuZi();
TuZi new_t;
tz.add(t);
int time = 0;
while(true) {
time++;
for(int i=0; i<tz.size(); i++) {
t = tz.get(i);
t.age++;
if(t.age > 3) {
new_t = t.produce();
tz.add(new_t);
}
}
System.out.println("第" + time + "个月的兔子数量为:" + tz.size());
if(tz.size() > 10000) break;
}
}
}