C 环路及;功能:如何计算积累一定数量的钱需要多少年才能退休?
我在C编程课的作业上遇到了点麻烦;我似乎被困在如何正确实现和终止程序中的循环上 这是我第一次问一个关于StackOverflow的问题,所以我请求你们耐心,因为我是一个编程高手;我一直有点害怕在这里发帖,以免看起来像个白痴P 以下是作业说明: “编写一个程序,计算积累一定数量的数据需要多少年。” 退休的钱 程序必须提示: -起始平衡 -每年存款额 -预计年收益利率(%) -目标余额 程序输出一个表格,显示每年的信息。计算 表与此处所述完全相同:C 环路及;功能:如何计算积累一定数量的钱需要多少年才能退休?,c,loops,while-loop,iteration,currency,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"
这是我到目前为止的悲伤代码(如果格式看起来很奇怪,很抱歉):
我非常感谢您的反馈
提前谢谢
while (endbalance != target);
你的循环是无限运行的,因为循环只有在它等于目标时才会终止,如果它超过目标,循环就会继续…这就是为什么它没有终止…有时endbalance可能会超过目标而不等于目标…所以,修改你的代码如下
while (endbalance <= target);
while(endbalance不是endbalance吗endbalance应该大于并等于目标值?正要发布相同的值:0)感谢这成功终止了循环,但我认为^SSC是正确的,末端平衡应该能够超出目标一点。为什么末端平衡应该大于目标…循环应该继续,直到末端平衡大于或等于目标,如果末端平衡等于或大于目标,循环终止s、 …所以,条件应该是EndBalance我解决了您的问题。谢谢您的帮助,但我仍然没有弄清楚如何将0年的初始存款和余额显示为250000。请在do while循环之前再打印一个printf。。。
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);
}