import java.util.ArrayList;
import java.util.List;
/**
* @author songwentao
* 2009-12-03
*/
public class Cow {
private int age = 0;
/**
* 是否可以生产小牛
* @return True | False
*/
public boolean isCreatSmallCow(){
return (age >= 3)?true:false;
}
/**
* 过年了,年龄又大了一岁
*/
public void addYear(){
age++;
}
/**
* @param args
*/
public static void main(String[] args) {
List<Cow> cowList = new ArrayList<Cow>();
//第一头小牛
cowList.add(new Cow());
int yearCount = 10;
//就这样一年年过
for(int i=1;i<=yearCount;i++){
int rowNum = cowList.size();
for(int j = 0; j<rowNum; j++){
Cow o = cowList.get(j);
//过年了
o.addYear();
//能生小牛吗?
if(o.isCreatSmallCow()){
cowList.add(new Cow());
}
}
}
System.out.println(yearCount+"年后将有【"+cowList.size()+"】头牛。");
}
}
------十年后有28头 |