public class Test {
static int year =0; //record the year
static double amount=0; //the total money you have at the end of year
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
calculate(1000*1.05);
System.out.println("According to what you said ,the year needed is "+(year+1));
}
public static void calculate(double total){
if(total>=1000000)
return;
amount = (2000+total)*1.05;
year++;
if(amount>=1000000)
return;
else
calculate(amount);
}
}
我的用递归有点复杂,楼主参考楼上的就好。