C ++嵌套循环初学者。我究竟做错了什么?

时间:2014-02-16 01:21:18

标签: c++

我尝试了各种编辑,但我没有看到我出错的地方。

我希望每年看起来与以下相似;然而,它似乎不适用于第一年和最后几年(只有中间的所有):

利率为11%,最终可用的金额 前十年的情况如下:
第1年:1120.00美元
第2年:1254.40美元
第3年:1404.93美元
第4年:1573.52美元
第5年:1762.34美元
第6年:1973.82美元
7年级:2210.68美元
8年级:2475.96美元
9年级:2773.08美元
第10年:$ 3105.85

请帮忙!

int main()
    {
    int interest, year;
    double amount;
    for (interest=6; interest <= 12; interest++)            //start of outer loop
    {
        amount = 1000;
        for (year=1; year <= 10; year++)                        // start of inner loop
        {
            amount = amount + interest * .01 * amount;
            cout << "End of year " << setw(2) << year << ": $" 
                 << setiosflags(ios::fixed) << setprecision(2) << amount << endl;
        }                                                           // end of inner for loop

        cout << "\nAt an interest rate of " << interest
         << "%, the amount of money available at the end\n"
         << "of the first ten years is as follows: \n" ;
    }                                                               // end of outer for loop
    return 0;
}

----------- -----------输出

第1年:$ 1060.00
第2年:1123.60美元 3年级:1191.02美元 4年级:1262.48美元 5年级:1338.23美元 6年级:1418.52美元 7年级:1503.63美元 8年级:1593.85美元 9年级:1689.48美元 10年级:1790.85美元

利率为6%,最后可用的金额为
前十年的情况如下:
1年级:1070.00美元 第2年:1144.90美元 3年级:1225.04美元 4年级:1310.80美元 5年级:1402.55美元 6年级:1500.73美元 7年级:1605.78美元 8年级:1718.19美元 9年级:1838.46美元 10年级:1967.15美元

利率为7%,最后可用的金额为
前十年的情况如下:
1年级:1080.00美元 第二年:1166.40美元 3年级:1259.71美元 4年级:1360.49美元 5年级:1469.33美元 6年级:1586.87美元 7年级:1713.82美元 8年级:1850.93美元 9年级:1999.00美元 10年级:2158.92美元

利率为8%,最终可用的金额 前十年的情况如下:
第1年:1090.00美元
第2年:1188.10美元
第3年:1295.03美元
第4年:1411.58美元
5年级:1538.62美元
第6年:1677.10美元
7年级:1828.04美元
8年级:1992.56美元
9年级:2171.89美元
第10年:2367.36美元

利率为9%,最终可用的金额 前十年的情况如下:
第1年:1100.00美元
第2年:1210.00美元
第3年:1331.00美元
4年级:1464.10美元
5年级:1610.51美元
第6年:1771.56美元
7年级:1948.72美元
8年级:2143.59美元
9年级:2357.95美元
第10年:2593.74美元

利率为10%,最终可用的金额 前十年的情况如下:
第1年:1110.00美元
第2年:1232.10美元
第3年:1367.63美元
4年级:1518.07美元
5年级:1685.06美元
第6年:1870.41美元
7年级:2076.16美元
8年级:2304.54美元
9年级:2558.04美元
第10年:2839.42美元

利率为11%,最终可用的金额 前十年的情况如下:
第1年:1120.00美元
第2年:1254.40美元
第3年:1404.93美元
第4年:1573.52美元
第5年:1762.34美元
第6年:1973.82美元
7年级:2210.68美元
8年级:2475.96美元
9年级:2773.08美元
第10年:$ 3105.85

利率为12%,最终可用的金额 前十年的情况如下:
按任意键继续。 。

2 个答案:

答案 0 :(得分:1)

试试这个:

int main()
{
    int interest, year;
    double amount;

    for (interest=6; interest <= 12; interest++)            //start of outer loop
    {

         cout << "\nAt an interest rate of " << interest
             << "%, the amount of money available at the end\n"
             << "of the first ten years is as follows: \n" ;

         amount = 1000;

         for (year=1; year <= 10; year++)                        // start of inner loop
         {

            amount = amount + interest * .01 * amount;
            cout << "End of year " << setw(2) << year << ": $" 
                  << setiosflags(ios::fixed) << setprecision(2) << amount << endl;

         }                                                           // end of inner for loop

     }                                                               // end of outer for loop

     return 0;

}

在这段代码中,循环显示&#34;以利率...&#34;声明之前它显示实际数据。在您的代码中,显示语句被颠倒过来。

答案 1 :(得分:0)

看起来它正在为其他利率工作,但事实并非如此。例如,7%文本之后的数字实际上是8%的数字。这样做的原因是在内部循环之后生成以“感兴趣”开头的文本,您需要将该位移动到经过这些年的for循环之前。

相关问题