0

好的,所以我有这门课:

package com.sandrovictoriaarena.unittesting;

public class LoanCalculator {
private double pricipelValue; //PV
private double interestRate; //rate
private int loanLength; //n
private double result;

public LoanCalculator(double pricipelValue, double interestRate,
        int loanLength) {
    super();
    this.pricipelValue = pricipelValue;
    this.interestRate = interestRate / 100;
    this.loanLength = loanLength;
}

public double getPricipelValue() {
    return pricipelValue;
}

public void setPricipelValue(double pricipelValue) {
    this.pricipelValue = pricipelValue;
}

public double getInterestRate() {
    return interestRate;
}

public void setInterestRate(double interestRate) {
    this.interestRate = interestRate;
}

public int getLoanLength() {
    return loanLength;
}

public void setLoanLength(int loanLength) {
    this.loanLength = loanLength;
}

@Override
public String toString() {
    return "LoanCalculator [pricipelValue=" + 
    pricipelValue + ", interestRate=" + interestRate + 
    ", loanLength=" + loanLength + "]";
}

public double calculateMonthlyPayment() throws IllegalArgumentException{
    result = (1 - (Math.pow((1 + interestRate), -1 * loanLength)));
    result = interestRate / result;
    result = result * pricipelValue;
    return result;
}

}

我正在制作一个具有以下值的对象: new LoanCalculator (100.0,20.0,6);

我运行 calculateMonthlyPayment() 时的结果应该是 17.65,但我一直得到 30.07。我究竟做错了什么?

4

1 回答 1

3

The code and the formula both are correct.

You are passing the interest rate as 20%, but it is probably 20% per annum. I think your interval is 6, which is probably six months. You should always pass the interest rate in the same terms as the number of intervals. Here, your number of intervals is in months, but your interest rate is in years (per annum). So just pass the interest rate as monthly rate, and you should get the right answer!

The monthly interest rate is 20%/12 = (0.2/12). If you substitute that in your input, you will get the right answer. So you should be doing: new LoanCalculator (100.0,20.0/12,6)

于 2013-09-21T18:35:24.747 回答