【发布时间】:2023-02-07 13:35:22
【问题描述】:
我目前正在为我的 C++ 课程上 Zybooks 课程,我们将复习 while 循环。在这个问题中,它要我计算一个银行账户需要多少年才能将其初始余额翻一番。还增加了年金供款。我的代码如下:
#include <iostream>
using namespace std;
int main()
{
const double RATE = 5;
const double INITIAL_BALANCE = 10000;
const double TARGET = 2 * INITIAL_BALANCE;
cout << "Annual contribution: " << endl;
double contribution;
cin >> contribution;
double balance = INITIAL_BALANCE;
int year = 0;
while (balance < TARGET)
{
year++;
double interest = balance * RATE / 100;
balance = balance + interest + contribution
}
cout << "Year: " << year << endl;
cout << "Balance: " << balance << endl;
return 0;
}
我用这个作为答案,但遇到了这个意想不到的结果:
`输出不同。请参阅下面的要点。
输入 100
你的输出
年度贡献: 年份:13 余额:20627.8
预期产出 年度贡献: 年份:13 余额:20527.8`
【问题讨论】: