循环和功能:如何计算累积一定数量的退休金需要多少年?

时间:2014-09-24 06:06:42

标签: c loops while-loop iteration currency

我的C编程课的作业有点麻烦;我似乎被困在如何正确实施&终止程序中的循环。

这是我第一次在StackOverflow上提问,所以我请耐心等待,因为我是编程菜鸟;我一直害怕在这里张贴,以免看起来像个白痴。 :P

以下是作业的说明:

“编写一个程序,计算累积给定数量的数据所需的年数 退钱的钱。

该计划必须提示:   - 开始平衡   - 每年存入的金额   - 估计的年利率(%)   - 目标余额

程序输出一个表格,显示每年的信息。计算中的每个条目 表完全按照这里描述的那样:

  • 年号
  • 当年存入的金额
  • 当年的利息。计算为(年初+的平衡 存款)*费率
  • 年末余额。是年初+存款+的余额 兴趣

当达到或超过目标时,循环必须停止,然后是摘要 显示行。用户输入,前几行输出和摘要示例 线如下:

Enter starting balance ($): 250000.0 
Enter amount deposited every year ($): 20000.0 
Enter estimated annual interest rate (%): 10.0 
Enter target balance ($): 2000000.0 

Year Deposit Interest Balance 
---- ------- -------- ------- 
   0 250000.00 0.00 250000.00 
   1 20000.00 27000.00 297000.00 
   2 20000.00 31700.00 348700.00 
   3 20000.00 36870.00 405570.00 
   4 20000.00 42557.00 468127.00 
   . . . 
   . . . 
In year ??, balance ????? reaches target 2000000.00"

到目前为止,这是我的悲伤代码(抱歉,如果格式看起来很奇怪):

/* CSCI 112; online class */
#include <stdio.h>

/* Week 6: Lab 2 - Program 2 (Retirement) */
void main(void) {
    int year;
    double balance, target, endbalance, deposit, rate, interest;

    printf("Enter starting balance ($): ");
    scanf("%lf", &balance);
    printf("Enter amount deposited every year ($): ");
    scanf("%lf", &deposit);
    printf("Enter estimated annual interest rate (\%%): ");
    scanf("%lf", &rate);
    printf("Enter target balance ($): ");
    scanf("%lf", &target);

    year = 0;
    interest = 0;
    rate = rate / 100.0;
    endbalance = balance + deposit + interest;

    printf("\nYear    Deposit    Interest    Balance");
    printf("\n----    -------    --------    -------");
    do {
        endbalance = endbalance + deposit + interest;
        printf("\n%d    %.2lf    %.2lf    %.2lf", year, deposit, interest, endbalance);
        year += 1;
        interest = (endbalance + deposit) * rate;
    } while (endbalance <= target); 

    printf("\nIn year %d, balance %.2lf reaches target %.2lf", year, balance, target);
}

输出:

Enter starting balance ($): 250000.0
Enter amount deposited every year ($): 20000.0
Enter estimated annual interest rate (%): 10.0
Enter target balance ($): 2000000.0

Year    Deposit    Interest    Balance
----    -------    --------    -------
0    20000.00    0.00    290000.00
1    20000.00    31000.00    341000.00
2    20000.00    36100.00    397100.00
3    20000.00    41710.00    458810.00
4    20000.00    47881.00    526691.00
5    20000.00    54669.10    601360.10
6    20000.00    62136.01    683496.11
7    20000.00    70349.61    773845.72
8    20000.00    79384.57    873230.29
9    20000.00    89323.03    982553.32
10    20000.00    100255.33    1102808.65
11    20000.00    112280.87    1235089.52
12    20000.00    125508.95    1380598.47
13    20000.00    140059.85    1540658.32
14    20000.00    156065.83    1716724.15
15    20000.00    173672.42    1910396.57
16    20000.00    193039.66    2123436.22
In year 17, balance 250000.00 reaches target 2000000.00

我真的很感激一些反馈! :d

提前致谢!

1 个答案:

答案 0 :(得分:1)

while (endbalance != target);    

你的循环无限运行,因为循环仅在它等于目标时终止,如果它超过目标,循环继续......这就是为什么它没有终止...有时终端可能会超过目标而不仅仅是目标...所以,修改你的代码......

while (endbalance <= target);

这是您更新的代码......

/* CSCI 112; online class */
#include <stdio.h>

/* Week 6: Lab 2 - Program 2 (Retirement) */
void main(void) 
{

    int year;
    double balance, target, endbalance, deposit, rate, interest;

    printf("Enter starting balance ($): ");
    scanf("%lf", &balance);
    printf("Enter amount deposited every year ($): ");
    scanf("%lf", &deposit);
    printf("Enter estimated annual interest rate (\%%): ");
    scanf("%lf", &rate);
    printf("Enter target balance ($): ");
    scanf("%lf", &target);

    year = 0;
    interest = 0;
    rate = rate / 100.0;
    endbalance = balance + deposit + interest;

    printf("\nYear    Deposit    Interest    Balance");
    printf("\n----    -------    --------    -------");
    printf("\n%d    %.2lf    %.2lf    %.2lf", year, deposit, interest, endbalance);
    do {
        endbalance = endbalance + deposit + interest;
        printf("\n%d    %.2lf    %.2lf    %.2lf", year, deposit, interest, endbalance);
        year += 1;
        interest = (endbalance + deposit) * rate;
    } while (endbalance <= target);

    printf("\nIn year %d, balance %.2lf reaches target %.2lf", year, balance, target);
}