2015-10-02 96 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); 
    }