2012-01-16 53 views
0

好吧,我還是Java的新手,我正在做這個東西的第九周。我將發佈學校項目的一些源代碼 - 這不是完整的源代碼 - 這部分代碼是我的循環來分期償還貸款。我試圖從菜單選擇(菜單打印到文本字段)或用戶輸入中攤銷。問題是,我無法弄清楚它是數學還是我的循環,它不是在正確地分期償還貸款。我只想知道是否有人看到我錯過的東西,如果有的話,請你指出來,以便我可以走上正確的軌道?提前致謝!使用Java swing組件循環分期償還貸款。

來源:

private void amortizeButtonActionPerformed(java.awt.event.ActionEvent evt) { 
    // This module borrowed from Ryan Jones in George Griepp's PRG 420 class. 

    NumberFormat nf = NumberFormat.getCurrencyInstance(); 
    int Monthly = 0; 
    int monthcount = 0; 
    String Output = ""; 

    int i = 0; // For first loop 
    double loanamount = Double.parseDouble(tempTextField3.getText());  //all loan amounts are the same 
    double rate = Double.parseDouble(tempTextField1.getText());  //Array for the rate 
    double time = Double.parseDouble(tempTextField2.getText());   //Array for the time 
    double totalnumpayments = 0;   //Set for 
    double monthlypayment = 0; //Set for math calculation in first loop 
    double interestPayment = 0;   //Set for math calculation in first loop 
    double totaltime = 0; //Set for second loop to know how long to loop 
    double loan = 0; //Set for second loop 
    double interestPayment2 = 0; //Set for second loop 
    double principlePayment = 0; //Set for second loop 


    for (i = 0; i < time; i++) {//First loop This loops through the arrays and gives the first message listed below three times 
    monthlypayment = (loanamount * ((rate/12)/(1 - Math.pow((1 + (rate/12)), -(time * 12))))); 
    interestPayment = loanamount * (rate * 100/1200); 
    totaltime = (time * 12); 


    jTextArea1.setText(""); 
    jTextArea1.setText("This loan has an interest rate of " + (rate * 100) + "%" + " and a starting loan amount of " + nf.format(loanamount)); 
    jTextArea1.setText("Payment Number\t\t" + "Towards Principle\t\t" + "Towards Interest\t" + "Remaining on loan"); 
    jTextArea1.setText(""); // Part of the first loop this will appear three times with the math listed above 


    System.out.println(totaltime); 
    Monthly++; 

    Output += ((monthcount++) + "\t\t\t" + nf.format(principlePayment) + "\t\t\t" + nf.format(interestPayment2) + "\t\t\t" + nf.format(loan - principlePayment) + "\n"); 


    loan = -principlePayment;// Changes the numbers as the loop goes 
    interestPayment2 = loan * (rate * 100/1200);// Changes the numbers as the loop goes 
    principlePayment = monthlypayment - interestPayment2;// Changes the numbers as the loop goes 

    } 
    jTextArea1.setText(Output); 
} 
+1

由於您正在學習,我認爲這是使用調試器進行學習的適當時機。 – 2012-01-16 07:34:14

+0

請學習java命名約定並堅持他們:-) – kleopatra 2012-01-16 07:42:41

+1

順便說一下,這是什麼問題? (如:預期/預期與實際行爲) – kleopatra 2012-01-16 07:44:38

回答

2

有幾件事情,這可能是一個問題或沒有(我不熟悉的背後loans的數學)。

  1. 你在你的for -loop計算totaltime,而不使用的i變量。因此,它總是會產生相同的結果,並且可以循環
  2. 外面來計算通過調用循環,你的最後一次通話是空StringjTextArea1setText,你還不如進入for之前就明確表示迴圈,因爲最後jTextArea1將只是空的。或者只是刪除代碼,因爲在for循環後,您將文本設置爲Output
  3. loan = -principlePayment是一個奇怪的聲明。因此,如果principlePayment是正數,loan將是一個負數,並且interestPayment2以及
  4. 你最好使用StringBuilder來構建你的Output變量

我覺得數字3是你最重要的計算。

+0

我應該刪除i變量並執行計算 - 或者甚至可以工作嗎? – 2012-01-16 18:10:30

+0

正如我所說,我不知道如何計算貸款。我只是在你的代碼中指出了奇怪的東西,它應該允許你使用調試器並調查你得到的結果是否是你期望的。但是,從循環中刪除我不會工作 – Robin 2012-01-16 18:19:11

+0

謝謝大家!羅賓您的評論得到了正確的區域。這是一個不合適的影響我的整個輸出。 – 2012-01-17 02:43:36