public Bee anotherDay(){
flower = garden.findFlower();
int pol = 5;
bool=flower.extractPollen(pol);
if(bool=true){
hive.addPollen(pol);
}else{
++pol;
bool=flower.extractPollen(pol);
if(bool=true){
hive.addPollen(pol);
}else{
++pol; //etc.
}
}
该代码的要点是:
1)use the findFlower() method on garden ot return a flower
2)use the extract pollen method on the flower with 5 as the initial paramater
3)If there isn't 5 pollen in the flower, the method returns false so try again with 4
4)If there isn't 4 try with 3 etc. until 0.
我正在考虑使用for循环,但如果方法成功并且返回true,我不知道如何突破它,所以我不会继续得到5 + 4 + 3 + 2 + 1花粉从花。
答案 0 :(得分:5)
flower = garden.findFlower();
for(int pol=5; pol>0; pol--)
{
if(flower.extractPollen(pol) {
hive.addPollen(pol);
break;
}
}