0

我的代码运行良好,除了每月贷款计算器的返回值。它继续为我的每月付款和总付款返回 Infinity。请帮忙计算公式。这是一个家庭作业。我只需要知道我是否错误地执行了公式。我感觉它以某种方式试图除以 0 然后返回无穷大,但我可能是错的。

public class MyLoan
{

private double amountBorrowed;
private double yearlyRate;
private int years;

public double A;
public double n = years * 12;

public MyLoan(double amt, double rt, int yrs)
{
    amountBorrowed = amt;
    yearlyRate = rt;
    years = yrs;
}
public double getAmountBorrowed()
{
    return amountBorrowed;
}

public double getYearlyRate()
{
    return yearlyRate;
}

public int getYears()
{
    return years;
}

public double monthlyPayment()
{
    double i = (yearlyRate / 100) / 12;

    A = (amountBorrowed) * (i * Math.pow(1+i, n)) / (Math.pow(1+i, n) -1);

    return A;
}

public double totalPayment()
{
    return A * (years * 12);
}

public String toString()
{
    return "Loan: " +  "$" + amountBorrowed + " at " + yearlyRate + " for " + years + " years";
}



public static void main(String[] args)
{

final double RATE15 = 5.75;
final double RATE30 = 6.25;


StdOut.println("***** Welcome to the Loan analyzer! *****");

String ans = "Y";

do {
  StdOut.print("\n  Enter the principle amount to borrow: ");
  double amount = StdIn.readDouble();


  MyLoan fifteenYears = new MyLoan(amount, RATE15, 15);
  MyLoan thirtyYears = new MyLoan(amount, RATE30, 30);

  double amount15 = fifteenYears.monthlyPayment();
  double total15 = fifteenYears.totalPayment();
  double amount30 = thirtyYears.monthlyPayment();
  double total30 = thirtyYears.totalPayment();

  StdOut.println("===========ANALYSES==========");
  StdOut.println(fifteenYears);
  StdOut.println("Monthly payment = " + "$" + amount15);
  StdOut.println("Total payment = " + "$" + total15);

  StdOut.println("");
  StdOut.println("");

  StdOut.println(thirtyYears);
  StdOut.println("Monthly payment = " + "$" + amount30);
  StdOut.println("Total payment = " + "$" + total30);
  StdOut.println("=============================");



  StdOut.print("\n      ** Do you want to continue (y/n)? ");
  ans = StdIn.readString();


} while (ans.toUpperCase().equals("Y"));

StdOut.println("\n********** Thank you. Come again! **********");

} 

}
4

2 回答 2

0

计算利息的方法有很多,最常见的就是

A = amountBorrowed * (yearlyRate / 100) / 12;
于 2013-10-03T17:40:02.557 回答
0

你应该自己调试这个,但我会给你一个提示。什么是 1^n(其中 n 是正整数)?在您的代码中,您在哪里使用此构造?

于 2013-10-03T17:40:11.163 回答