gpt4 book ai didi

java - 我在生成贷款配置时缺少哪些循环?

转载 作者:行者123 更新时间:2023-12-01 09:57:33 24 4
gpt4 key购买 nike

我的java程序应该接受硬编码的贷款信息并产生一个输出,显示1-60天内的付款、余额、付款金额、支付的利息、本金申请和新余额。我在下面的代码中我需要帮助的区域添加了注释。另外,我现在的输出格式不整齐。任何帮助将不胜感激。

public static void main(String[] args) {
// TODO Auto-generated method stub

double loanAmount = 25000.00;
double annualInt = 0.0525;
double numberOfMonths = 60.00;
double monthlyPayment;
double rate;
double balance;
double intPaid=0;
double prAppl=0;
int paymentsMade;


System.out.println("Principal : "+loanAmount);
System.out.println("Annual Int: "+annualInt*100+"%");
System.out.println("Term (mo) : "+numberOfMonths);


paymentsMade = numOfPaymentsMade(numberOfMonths);
rate = calcMonthlyInterestRate(annualInt);
monthlyPayment = calcMonthlyPayment(rate,loanAmount,numberOfMonths);
balance = calcBalance(loanAmount,rate,numberOfMonths,monthlyPayment);
intPaid = interestPaid(rate,balance,intPaid);
prAppl = prApp(monthlyPayment,intPaid,prAppl);
displayResults(numberOfMonths,rate,loanAmount,paymentsMade,balance,intPaid,monthlyPayment,prAppl);

}
public static int numOfPaymentsMade(double numberOfMonths)
{
int paymentsMade=0;
for(paymentsMade=1; paymentsMade<numberOfMonths+1; paymentsMade++)
{
System.out.println(paymentsMade);
}

return paymentsMade;
}
//--------------------------------------------------------------------------------------------------------
public static double calcMonthlyInterestRate(double annualInterestRate){
double monthlyInterestRate;
monthlyInterestRate = (annualInterestRate/12);
return monthlyInterestRate;
}
//--------------------------------------------------------------------------------------------------------
public static double calcMonthlyPayment(double rate, double loanAmount, double numberOfMonths )
{
double monthlyPayment;
int i;

monthlyPayment = (((rate)*(loanAmount))/(1-(Math.pow(1+(rate),-1*(numberOfMonths)))));

for(i=0; i<60; i++)
{
System.out.println(monthlyPayment);
}
return monthlyPayment;
}
//--------------------------------------------------------------------------------------------------------
public static double calcBalance(double loanAmount, double rate, double numberOfMonths, double monthlyPayment)
{
double balance=0;
// this method needs fixing

for(balance=loanAmount; balance<monthlyPayment-1; balance--){
balance = (loanAmount-monthlyPayment)+(balance*rate);
}
return balance;
}
//--------------------------------------------------------------------------------------------------------
public static double interestPaid(double rate, double balance, double intPaid)
{
// need a loop corresponding with the balance
intPaid=balance*rate;

return intPaid;
}
//--------------------------------------------------------------------------------------------------------
public static double prApp(double monthlyPayment, double intPaid, double prAppl)
{
// need a loop corresponding with intPaid
prAppl=monthlyPayment-intPaid;

return prAppl;
}
//--------------------------------------------------------------------------------------------------------
public static void displayResults (double numberOfMonths, double rate, double loanAmount, double paymentsMade, double balance, double intPaid, double monthlyPayment, double prAppl){
double monthPay;
monthPay = (((rate)*(loanAmount))/(1-(Math.pow(1+(rate),-1*(numberOfMonths)))));

System.out.println("Payment :"+monthPay);
System.out.println("");
System.out.println("Pmt Balance Payment Int Pd Prin Appl New Bal");
System.out.println("--- ---------- --------- -------- --------- ----------");
System.out.println(paymentsMade+" "+balance+" "+monthlyPayment+" "+intPaid+" "+prAppl);

//maybe a method should be added for the new balance section
//how do I add all of the computations up?

} }

最佳答案

这个循环存在三个问题:

    for(balance=loanAmount; balance<monthlyPayment-1; balance--){
balance = (loanAmount-monthlyPayment)+(balance*rate);
}

首先,balance-- 从余额中减去 1。你没有理由想要这样做(好吧,如果抵押权人是银行家的亲戚,每月无偿地减少一点他们的余额将是一个很好的小礼物,但否则你不会想这样做)。绝大多数 for 循环都涉及每次循环增加 1 或减少 1 的索引。但这里的情况并非如此,因此您不需要 balance--。循环体应该减少平衡,因此循环的第三部分根本不需要任何东西。你可以简单地写:

for (balance = loanAmount; balance < monthlyPayment - 1; ) {

但我倾向于使用 while 循环。上面相当于:

balance = loanAmount;
while (balance < monthlyPayment - 1) { ... }

这意味着只要余额低于每月付款,循环就会继续进行。

这是第二个问题。您希望只要余额高于每月付款就继续循环,并在低于每月付款时停止。这意味着 for 循环中的条件是向后的(与我的 while 循环示例中的条件相同)。您可能想要

    for (balance = loanAmount; balance >= monthlyPayment - 1; ) {

第三个问题是体内的公式错误。您想要确定付款中有多少是本金,然后将余额减少该金额。你的循环体几乎做到了这一点,但有一个很大的错误。一旦你解决了循环头的问题,我会让你解决这个问题。

关于java - 我在生成贷款配置时缺少哪些循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37064954/

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