2012-01-16 74 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