这个 c++ 代码在一年之后不会停止。它应该停在 1331(它第一次说已保存)。希望我是使用递归的新手,我不只是缺少一些基本的东西。提前谢谢你。我尝试了多种无效的更改。
Sample input:
- Enter inital amount to save: 1000
- Enter yearly interest rate (e.g. 0.10 for 10): .1
- Enter number of years of compounded interest: 3
Output:
iAmount: 1100
Years: 3
iAmount: 1210
Years: 2
iAmount: 1331
Years: 1
Saved up: 1331
Saved up: 1210
iAmount: 1210
Years: 1
Saved up: 1210
Saved up: 1100
void interest(int iAmount, double rate, int years, double saved){
saved = iAmount * (1+rate);
iAmount = saved;
cout << "iAmount: " << iAmount << "\n";
cout << "Years: " << years << "\n";
while(years>1){
years = years - 1;
interest(iAmount, rate, years, saved);
}
if(years == 1){
cout << "Saved up: " << iAmount << "\n";
return;
}
}