2011-05-13 206 views
0

這是我的作業,它是由於星期一16日。 我終於得到了幾個月顯示正確,但數額是錯誤的。 此外,沒有必要,但它會很好停下來,並在每筆貸款之間打印一些東西。 像貸款1,貸款2,貸款3 ...需要幫助Java攤銷表計算

任何幫助將不勝感激

/*Write the program in Java (without a graphical user interface) 
and have it calculate the payment amount for 3 mortgage loans: 

- 7 year at 5.35% 
- 15 year at 5.5% 
- 30 year at 5.75% 

Use an array for the different loans. 
Display the mortgage payment amount for each loan 
and then list the loan balance and interest paid for 
each payment over the term of the loan. 
Use loops to prevent lists from scrolling off the screen.*/ 

我有個正確的,但貸款金額是錯誤的。

import java.io.IOException;  //Code that delays ending the program 


    class MonthlyRhondav4 
    {  

    public static void main (String[] args) throws IOException{ 

    double loanAmount = 200000.00; // $ amount borrowed 
    double monthlyPayment = 0; // monthly payment for calculating 
    double loanBalance; 
    double interestPaid; 
    double principalPaid; 
    int paymentCounter; 
    int lineCounter = 0; 



    java.text.DecimalFormat dcm = new java.text.DecimalFormat("$,###.00");          


    int termArray[] = {84, 180, 360};  // Different loan terms in months 
    double interestArray[] = {0.0535, 0.055, 0.0575};// Different interest rates for the loan 
    int k =0;// gonna be paymentIndex 

    /*Code to start the payment list*/ 

    System.out.print("\n\nPlease Press Enter to Continue to the 3 Different Amortization Lists"); 
    System.out.println(); 
    System.out.println(); 

    System.in.read(); 
    System.in.read(); 

    /*Display columns*/  

    System.out.println("Month \t Loan Amount Left\tInterest\t\tPrincipal \n"); //Prints headers for columns 
    System.out.println(); 


    /*Loop to calculate and print monthly payments*/ 

    //for(k=0; k<3; k++){// k is going to be paymentIndex to loop through index 

    for (k = 0; k < interestArray.length; k++) { 

     for(paymentCounter =1; paymentCounter <= termArray[k]; paymentCounter++) // months through array 
     { 

     /********TROUBLE HERE***************************************************************************************/ 

      monthlyPayment = ((loanAmount * (interestArray[k]) * termArray[k]) + loanAmount)/(termArray[k] * 12); 

      interestPaid = loanAmount*(interestArray[k]/12);   //interest paid through array 

      principalPaid = monthlyPayment-loanAmount*(interestArray[k]/12);  //principal paid 

     /*need to fig monthly payment+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ 




      System.out.println(paymentCounter + "\t" + dcm.format(loanAmount) + "\t\t" + dcm.format(interestPaid) + "\t\t\t" + dcm.format(principalPaid)); 



      lineCounter++;        //Increment the display counter 
      if (lineCounter > 11 && paymentCounter < termArray[k]*12) //Check to see if 12    
      { 

       System.out.println ("Please Press Enter to Continue the List"); //Code to delay ending the program 
       System.in.read(); 
       System.in.read(); 
       lineCounter = 0; 

      } 

     } 
     loanAmount = (loanAmount - (monthlyPayment-loanAmount*(interestArray[k]/12))); //Calculate new loan amount 
    } 


    }//ends public static void main 
    }//ends public class 

回答

0

一個觀察 - 你沒有做與貸款餘額東西。之所以沒有什麼變化,是因爲在計算了給定付款的利息和本金額後,您並未通過付款的主要部分減少貸款餘額。您需要更改您的代碼以顯示當前的貸款餘額,並計算當前貸款餘額中的本金/利息拆分,而不是原始貸款金額。


編輯

好 - 我看你是要更新的平衡,但你有它的循環的貸款之外。這需要在循環內部進行,以便爲每次付款進行更新。此外,你一次又一次地擁有諸如loanAmount * (interestArray[k]/12)之類的東西。考慮使用變量,如

double interestPaid = loanAmount * (interestArray[k]/12) 

這將使您的代碼更易於閱讀,更易於維護,因爲如果你發現在計算一個錯誤,你只需要固定在一個地方的錯誤,而不是修復它你到處都有計算。

我也看不到您計算每月付款的位置。這是原貸款金額,付款數量和利率的函數。請記住,每月付款是固定的,並且每筆付款的利息/本金拆分將隨着貸款的支付而變化。您可能會發現http://en.wikipedia.org/wiki/Mortgage_calculator有助於找出每月付款的公式。

+0

確定它的工作需要一個小的休息interest = interestRate/100; 月=年* 12; monthlyPayment =(loanAmount *(interest/12))/(1 - 1 /Math.pow((1+interest/12),months)); – rhonda 2011-05-13 23:43:53

+0

由於'interestArray'中的費率已經是小數,而不是百分比,所以不要用'100'除。 – QuantumMechanic 2011-05-13 23:47:04

0

第一:從來沒有使用雙計算的東西......這是一個建議,你必須記住。如果你不想相信我,請在Google上搜索「java double computation」或類似的東西,你會看到的。或者閱讀優秀圖書「有效的Java」

BigDecimal類是那裏有正確的數字在Java中

+1

對於這樣的情況,'BigDecimal'是矯枉過正,只會讓事情變得更加複雜。在生產代碼中,您必須準確處理貨幣金額,這是另一回事。 – QuantumMechanic 2011-05-13 23:20:00

+0

我知道肯定......但是最好早點知道,不遲? – Grooveek 2011-05-13 23:25:26

+0

嘿,我相信你。你能告訴我我在哪裏使用雙重計算? – rhonda 2011-05-13 23:26:56

0

很多錯誤。例如,您從未將monthlyPayment設置爲除0之外的任何值。您也不使用loanBalanceloanAmount應該是一個常數。您還可以簡化冗餘計算。示例:

interestPaid = loanBalance*(interestArray[k]/12); 
principalPaid = monthlyPayment-interestPaid; 

代替

interestPaid = loanBalance*(interestArray[k]/12); 
principalPaid = monthlyPayment-loanBalance*(interestArray[k]/12); 

我也是不知道你有正確的利率公式,但我不會去檢查,直到刪除了一些比較明顯的錯誤。