我修复了你们指出的问题(谢谢顺便说一句!)但是现在它给了我一个无限循环。 我不明白为什么。每次while循环运行时,我的抵押贷款数量都会逐月减少...
#include <stdlib.h>
int main(){
float MortgageLeft, InterestRate, MonthlyPayment, MonIntRate, AmountOwed;
int Month=0;
printf("What is the value left on the mortgage?\n");
scanf("%f", &MortgageLeft);
printf("What is the annual interest rate of the loan, in percent?\n");
scanf("%f", &InterestRate);
printf("What is the monthly payment?\n\n");
scanf("%f", &MonthlyPayment);
MonIntRate= (InterestRate/12)/100;
printf("Month\t\t Payment\t\t Amount Owed");
while (MortgageLeft>0){
MortgageLeft=(MortgageLeft*MonIntRate)+MortgageLeft;
if(MortgageLeft>MonthlyPayment)
{
MortgageLeft=MortgageLeft-MonthlyPayment;
Month++;
printf("%d\t\t %.2f\t\t %.2f", Month, MonthlyPayment, MortgageLeft);
}
}
return 0;
}
答案 0 :(得分:2)
你的while
循环没有任何括号,所以它只执行循环中的下一个语句AmountOwed=(MortgageLeft*MonIntRate)+MortgageLeft;
,它永远不会改变循环条件。无限循环意味着您永远不会到达if
/ else
。
答案 1 :(得分:2)
你不会离开While循环,直到MortgageLeft小于或等于零。在什么时候,While循环是MortgageLeft的值越来越小?
对于您的更新问题,当MortgageLeft小于或等于MonthlyPayment但仍然大于零时会发生什么?
答案 2 :(得分:0)
在以下代码中,您要么缺少While循环的花括号和/或错过修改MortgageLeft
的值
while (MortgageLeft>0)
AmountOwed=(MortgageLeft*MonIntRate)+MortgageLeft;
if(AmountOwed>MonthlyPayment)
{
AmountOwed=AmountOwed-MonthlyPayment;
Month++;
printf("%d\t\t %.2f\t\t %.2f", Month, MonthlyPayment, AmountOwed);
}
else
{
Month++;
printf("%d\t\t %f\t\t 0", Month, AmountOwed);
}