2015-10-02 64 views
-1

我解决了你们指出的问题(谢谢btw!),但现在它给了我一个无限循环。 我不明白为什么。我mortgageleft是越来越每隔while循环运行时间monthlypayment下降...If Else not running

#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; 
} 
+1

值检查括号 –

+0

您应该包括您所使用的语言标签花括号。 – brenners1302

+1

您并未更改“MortgageLeft”值。那个'while'循环何时会退出? – DarkKnight

回答

2

while环没有任何括号,所以它只能在循环,这永远不会改变循环条件执行第二天声明AmountOwed=(MortgageLeft*MonIntRate)+MortgageLeft; 。无限循环意味着你永远不会到达if/else

2

在MortgageLeft小于或等于零之前,您不会离开While循环。在While循环中,MortgageLeft的价值越来越小?

对于您更新的问题,MortgageLeft小于或等于MonthlyPayment时会发生什么,但仍大于零?

+0

谢谢,我现在看到了。 –

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); 
    }