gpt4 book ai didi

java - 贷款利率返回错误值?

转载 作者:行者123 更新时间:2023-12-01 23:34:14 26 4
gpt4 key购买 nike

好吧,我有这门课:

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

}

我正在制作一个具有以下值的对象:新贷款计算器(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)

关于java - 贷款利率返回错误值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18936072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com