【问题标题】:Don't understand why output is incorrect不明白为什么输出不正确
【发布时间】: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`

【问题讨论】:

    标签: c++ output


    【解决方案1】:

    我看到了预期的输出,你的输出相差 100,即你的contribution。一旦达到目标,评估系统可能不会增加年度贡献。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多