[英]Why is my method returning infinity?
十五.monthyPayment()); 每次運行它都會返回無窮大。 我不知道為什么。 我認為這與年份有關,因為如果我將年份值更改為等於某個數字(例如15),它將不會返回無窮大。 我認為MyLoan十五應該對我來說變成15。
誰能告訴我為什么這段代碼返回無窮大?
public class MyLoan {
// instance members
private double amountBorrowed;
private double yearlyRate;
private int years;
public double A;
public double n = years * 12;
// public instance method
public MyLoan(double amt, double rt, int yrs) {
this.amountBorrowed = amt;
this.yearlyRate = rt;
this.years = yrs;
}
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 static void main(String[] args) {
double RATE15 = 5.75;
double RATE30 = 6.25;
double amount = 10000;
}
MyLoan fifteen = new MyLoan(amount, RATE15, 15);
System.out.println(fifteen.monthlyPayment());
}
您需要在構造函數中初始化n
。
this.years = yrs;
this.n = yrs * 12;
否則,它將使用years
的默認值0
。
被0
的結果為Double.POSITIVE_INFINITY
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.