public int getAllPeach(int lastPeach,int day){// 使用for循环
int peachTotal=1;
for(int i=1;i<day;i++){
peachTotal=(peachTotal+1)*2;
}
return peachTotal;
}
public int getPeach(int lastPeach,int day){ //使用while循环
int dayNow =1;
int peachNow=1;
while(dayNow<day){
peachNow =(peachNow+1)*2;
dayNow++;
}
return peachNow;
}
public int getPeachAll(int lastPeach,int day,int dayNow){//使用递归
if(dayNow<=day){
System.out.println(dayNow+"天前,还有"+lastPeach+"个桃子");
dayNow++;
lastPeach =(lastPeach+1)*2;
getPeachAll(lastPeach,day,dayNow);
}
return lastPeach;
} |