我已经在这个项目上工作了大约一个星期,而我似乎无法正确地计算数学。 它的输出值,而不是正确的值。我已经多次查看过我的价值观了 数数。我绝望了!!我的程序计算了多年来长度的年度利益 贷款是。其次,它计算每年原始贷款需要支付多少钱 贷款总长度。
import java.util.Scanner;
public class mortgageCalc {
public static void main(String[] args)
{
// Other declarations for main program go here
Scanner keyboard = new Scanner(System.in);
double annualInterest;
int loanLength;
double loanAmount;
// Step 1: prompt for, read in, and store the input values.
System.out.print("Enter loan amount: ");
loanAmount = keyboard.nextDouble();
System.out.print("Enter length of the loan: ");
loanLength = keyboard.nextInt();
System.out.print("Enter interest rate as percent: ");
annualInterest = keyboard.nextDouble();
double newMonthlyPayment = payment(annualInterest,loanLength,loanAmount);
double totalInterest = (newMonthlyPayment * loanLength*12)-loanAmount;
printTotals(newMonthlyPayment, totalInterest,annualInterest,loanLength,loanAmount);
double balance = loanAmount;
double monthlyInterestRate = annualInterest/1200;
for (int year = 1; year <= loanLength; year++)
{
double remainingMonthlyInterest = getMonthlyInterest (balance, monthlyInterestRate);
double remainingInterest = totalInterest + remainingMonthlyInterest;
balance = getRemainingBalance(balance, newMonthlyPayment, remainingMonthlyInterest);
System.out.println("Monthly interest:"+remainingMonthlyInterest);
System.out.println("new balance:"+balance);
}
} // end of main
public static double payment(double annualInterest, int loanLength, double loanAmount)
{
double i = (annualInterest/1200);
double n = (loanLength*12);
double e1 = i+1;
double e2 = Math.pow(e1,n);
double e3 = e2-1;
double e4 = i * e2;
double e5 = e2-1;
double monthlyPayment = loanAmount*(e4/e5);
return monthlyPayment;
} // end of function payment
public static void printTotals(double newMonthlyPayment, double totalInterest,double
annualInterest,int loanLength, double loanAmount)
{
System.out.print("For a "+loanLength);
System.out.printf(" year loan of $%5.2f ",loanAmount);
System.out.printf("at %5.2f ",annualInterest);
System.out.println("interest--");
System.out.printf("Monthly payment: $ %5.2f\n",newMonthlyPayment);
System.out.printf("Total interest: $ %6.2f\n",totalInterest);
} // end of function printTotals
public static double getMonthlyInterest (double balance, double monthlyInterestRate)
{
double monthlyInterest=0.0;
for(int month=1; month<=12; month++)
{
monthlyInterest = balance * monthlyInterestRate;
}
return monthlyInterest;
}
public static double getRemainingBalance (double balance, double newMonthlyPayment,
double monthlyInterest)
{
double newBalance=0.0;
for(int month=1; month<=12; month++)
{
newBalance = balance-(newMonthlyPayment-monthlyInterest);
}
return newBalance;
}
} // end of class MortgageCalc
答案 0 :(得分:0)
您的getMonthlyInterest()
方法似乎不正确。虽然我不确定数学应该是什么,但您目前所拥有的内容将迭代12次(每月一次)并且每次完全重置monthlyInterest
为balance*monthlyInterestRate
你应该没有for循环,或者更准确地说应该每月将monthlyInterest添加到自己:
monthlyInterest = monthlyInterest + balance*monthlyInterestRate;
与您的getRemainingBalance()
方法相同。它将在每月for循环的每次迭代中重置。要么摆脱for循环,要么确保你的逻辑是正确的 - newBalance
使用的信息在循环过程中每个月都会发生变化。