好了,所以我有这个课:

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;
}

}


而且我正在制作具有以下值的对象:
新的LoanCalculator(100.0,20.0,6);

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

最佳答案

代码和公式均正确。

您将利率设为20%,但可能是每年20%。我认为您的时间间隔是6,大约六个月。您应该始终以与间隔数相同的条件来传递利率。在这里,您的间隔数以月为单位,但是利率以年(每年)为单位。因此,只需将利率作为月利率,就可以得到正确的答案!

每月利率为20%/ 12 =(0.2 / 12)。如果将其替换为输入内容,则将获得正确的答案。因此,您应该这样做:new LoanCalculator (100.0,20.0/12,6)

10-08 01:59