其实题目很简单,就是要求写一个能计算你的存款能用多少年多少月的程序。
要程序问你一共有多少存款,你每个月需提款多少,你每年的存款利息是多少,最后算出你所有的存款能用多少年多少月。
我可以写到让程序算出你第一月提款后还剩下多少钱,,,但不知道怎么让他一直计算到我所需的提款少于我的存款,然后计算出我的存款能用多少年多少月。。。我不懂我的while哪里用的不对了。。。。55555555555555555555
请帮忙指点一下吧。。。谢谢
leftover=剩余存款
withdraw=每月提款
nominalrate=年利息
principle=全部存款
interest=每月所得利息
#include <stdio.h>
#include <math.h>
int main(void)
{
float nominalrate, principle, withdraw, interest, leftover;
int months=0, years=0;
printf("This program is a retirement calculator, it calculates how long your savings will last. Please press enter to continue.\n");
printf("Please enter your total savings in dollar: \n");
scanf("%f",&principle);
printf("Please enter the amount of money withdrawn from savings each month: \n");
scanf("%f",&withdraw);
printf("Please enter your yearly interest rate: ");
scanf("%f",&nominalrate);
nominalrate *= 0.01; /*把百分比换算成小数点*/
interest = (principle-withdraw)*nominalrate/12; /*每月所得利息计算*/
leftover = principle-withdraw+interest; /*每月月底的剩余存款计算*/
while(leftover >= withdraw){
if(++months>12) {
months=1;
years ++;
}
}
if(leftover < withdraw){
printf("Your current savings will last %d years and %d months.\n", years,months);
}
return 0;
}
加粗部分是我不知道怎么写的部分。。。。。
[此贴子已经被作者于2007-9-9 21:05:00编辑过]