在C ++中,我无法获得以下代码来正确计算兴趣。应该是这样计算钱的。
1:1000
2:2050(1000(1.05)+ 1000)
3:3152.5(2050(1.05)+ 1000)
4:4310.125(3152.5(1.05)+ 1000)
但是,它却像这样计算我的兴趣。
1:1000
2:2050
3:4310.12
4:8142.01
看结果,我真的不知道发生了什么。
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main(){
bool done;
char finished;
int numberOfYears;
double accountBalance = 1000;
int i;
const double rateOfInterest = 1.05;
const int yearlyIncome = 1000;
while (!done){
cout << "Enter the number of years you've kept your\n"
<< "money in your bank account." << endl;
cin >> numberOfYears;
for (i = 1; i < numberOfYears; i++){
accountBalance *= rateOfInterest;
accountBalance += yearlyIncome;
}
cout << "Balance = $" << accountBalance << endl;
cout << "If you are finished, enter y, otherwise,\n"
<< "enter any key." << endl;
cin >> finished;
if (finished == 'y' || finished == 'Y') done = true;
}
return 0;
}
答案 0 :(得分:0)
问题在于您使用该程序的方式。您无需在每次计算后将accountBalance
重置为1000。在开始while循环后,只需将double accountBalance=1000;
移至第一行。
答案 1 :(得分:0)
while循环不断记住acountBalance。我检查了其余代码是否正确。
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log(`${request.method} '${request.urlWithParams}' ProgressInterceptor ++.`);
this.progressService.increase();
return next.handle(request)
.pipe(
finalize(() => {
console.log(`${request.method} '${request.urlWithParams}' ProgressInterceptor --.`);
this.progressService.decrease();
}),
);
}