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花粉。
最佳答案
flower = garden.findFlower();
for(int pol=5; pol>0; pol--)
{
if(flower.extractPollen(pol) {
hive.addPollen(pol);
break;
}
}