【发布时间】:2016-05-06 05:32:37
【问题描述】:
我的 Java 程序假设接收硬编码的贷款信息并生成一个输出,显示在 1-60 天内的付款、余额、付款金额、支付的利息、本金申请和新余额。我将 cmets 放在下面我需要帮助的代码中。另外,我现在的输出格式不整齐。任何帮助将不胜感激。
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?
} }
【问题讨论】:
-
看起来不错,只是需要正确的逻辑xD
-
为什么
calcMonthlyPayment打印相同的数字 60 次?这似乎没什么用。 -
这样图表的输出在所有 60 个月中都是一致的。