gpt4 book ai didi

java - 使用带有 Java swing 组件的循环来摊销贷款。

转载 作者:行者123 更新时间:2023-12-02 08:01:13 24 4
gpt4 key购买 nike

好吧,我对 Java 还很陌生,我已经进入了学习这些东西的第九周了。我将发布一些来自学校项目的源代码 - 这不是完整的源代码 - 这段代码是我分期偿还贷款的循环。我试图从菜单选择(菜单打印到文本字段)或用户输入中摊销。问题是,我无法弄清楚这是数学问题,还是我的循环,是它没有正确摊销贷款。我只是想知道是否有人看到我错过的明显的东西,如果是的话,请您指出,以便我能走上正确的轨道?提前致谢!

来源:

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

最佳答案

有一些事情,可能是个问题,也可能不是问题(我不熟悉 loans 背后的数学原理)。

  1. 您在 for 循环中计算了 totaltime,而没有使用 i 变量。因此它总是会产生相同的结果,并且可以在循环外计算
  2. 通过在循环内的 jTextArea1 上调用 setText,其中最后一次调用使用空的 String,您不妨清除它在进入 for 循环之前,最终 jTextArea1 将是空的。或者只是删除代码,因为在 for 循环之后,您将文本设置为 Output
  3. loan = -principlePayment 是一个奇怪的说法。因此,如果 principlePayment 为正数,loan 将为负数,interestPayment2 也是负数
  4. 您最好使用 StringBuilder 来构造您的 Output 变量

我认为第三点对于您的计算来说是最重要的。

关于java - 使用带有 Java swing 组件的循环来摊销贷款。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8876360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com