这是我的作业,它将于周一16日到期。 我终于有了几个月的展示权,但金额是错误的。 此外,没有必要,但在每笔贷款之间停止和打印一些东西会很好。 像贷款1,贷款2,贷款3 ......
任何帮助将不胜感激
/*Write the program in Java (without a graphical user interface)
and have it calculate the payment amount for 3 mortgage loans:
- 7 year at 5.35%
- 15 year at 5.5%
- 30 year at 5.75%
Use an array for the different loans.
Display the mortgage payment amount for each loan
and then list the loan balance and interest paid for
each payment over the term of the loan.
Use loops to prevent lists from scrolling off the screen.*/
我的月份是正确的,但贷款金额是错误的。
import java.io.IOException; //Code that delays ending the program
class MonthlyRhondav4
{
public static void main ( String[] args) throws IOException{
double loanAmount = 200000.00; // $ amount borrowed
double monthlyPayment = 0; // monthly payment for calculating
double loanBalance;
double interestPaid;
double principalPaid;
int paymentCounter;
int lineCounter = 0;
java.text.DecimalFormat dcm = new java.text.DecimalFormat("$,###.00");
int termArray[] = {84, 180, 360}; // Different loan terms in months
double interestArray[] = {0.0535, 0.055, 0.0575};// Different interest rates for the loan
int k =0;// gonna be paymentIndex
/*Code to start the payment list*/
System.out.print("\n\nPlease Press Enter to Continue to the 3 Different Amortization Lists");
System.out.println ();
System.out.println ();
System.in.read();
System.in.read();
/*Display columns*/
System.out.println("Month \t Loan Amount Left\tInterest\t\tPrincipal \n"); //Prints headers for columns
System.out.println ();
/*Loop to calculate and print monthly payments*/
//for(k=0; k<3; k++){// k is going to be paymentIndex to loop through index
for (k = 0; k < interestArray.length; k++) {
for(paymentCounter =1; paymentCounter <= termArray[k]; paymentCounter++) // months through array
{
/********TROUBLE HERE***************************************************************************************/
monthlyPayment = ((loanAmount * (interestArray[k]) * termArray[k]) + loanAmount) / (termArray[k] * 12);
interestPaid = loanAmount*(interestArray[k]/12); //interest paid through array
principalPaid = monthlyPayment-loanAmount*(interestArray[k]/12); //principal paid
/*need to fig monthly payment+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
System.out.println(paymentCounter + "\t" + dcm.format(loanAmount) + "\t\t" + dcm.format(interestPaid) + "\t\t\t" + dcm.format(principalPaid));
lineCounter++; //Increment the display counter
if (lineCounter > 11 && paymentCounter < termArray[k]*12) //Check to see if 12
{
System.out.println ("Please Press Enter to Continue the List" ); //Code to delay ending the program
System.in.read();
System.in.read();
lineCounter = 0;
}
}
loanAmount = (loanAmount - (monthlyPayment-loanAmount*(interestArray[k]/12))); //Calculate new loan amount
}
}//ends public static void main
}//ends public class
答案 0 :(得分:0)
第一:永远不要用双倍来计算某些东西......这是你必须要记住的建议。如果您不想信任我,请在Google上搜索“java double calculation”或类似内容,您会看到。或者阅读优秀的书“Effective Java”
BigDecimal类在Java中有正确的数字
答案 1 :(得分:0)
一个观察 - 你没有对贷款余额做任何事情。没有任何变化的原因是,在计算了给定付款的利息和本金额之后,您不会通过付款的主要部分减少贷款余额。您需要更改代码以显示当前的贷款余额,并计算当前贷款余额的本金/利息分配,而不是原始贷款金额。
<强>被修改强>
好的 - 我看到你正在尝试更新余额,但你已经在贷款循环之外了。这需要在循环内部,以便为每次付款更新。此外,您还有一遍又一遍loanAmount * (interestArray[k] / 12)
之类的内容。考虑使用变量,例如
double interestPaid = loanAmount * (interestArray[k] / 12)
这将使您的代码更易于阅读和更易于维护,因为如果您在计算中发现错误,您只需要在一个地方修复错误,而不必在计算的任何地方修复它。
我也看不到你在哪里计算每月付款。这是原始贷款金额,付款数量和利率的函数。请记住,每月付款是固定的,每笔付款的利息/本金分配将随着贷款的减少而变化。您可能会发现http://en.wikipedia.org/wiki/Mortgage_calculator对于确定每月付款的公式非常有用。
答案 2 :(得分:0)
很多错误。例如,您从未将monthlyPayment
设置为除0之外的任何值。您也不使用loanBalance
。 loanAmount
应该是一个常数。您还可以简化冗余计算。例如:
interestPaid = loanBalance*(interestArray[k]/12);
principalPaid = monthlyPayment-interestPaid;
而不是
interestPaid = loanBalance*(interestArray[k]/12);
principalPaid = monthlyPayment-loanBalance*(interestArray[k]/12);
我也不确定你是否有正确的利率公式,但在你删除一些更明显的错误之前我不会检查。