作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的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/
我正在尝试编写一个 SQL 查询来根据以下公式计算 future 贷款余额: FV( future 余额)= CV(当前余额)- P(固定还款)+ I (利息资本化) 其中 **I = (CV * A
我是一名优秀的程序员,十分优秀!