java - Why is my method returning infinity? -


the fifteen.monthypayment()); returning infinity every time run it. cannot figure out why. believe has years, because if change years value equal number, 15, not return infinity. thought myloan fifteen should change 15 me.

can body tell me why code returning infinity?

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 = (yearlyrate / 100) / 12;          = (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());  } 

you need initialise n in constructor.

this.years = yrs; this.n = yrs * 12; 

otherwise uses default value years, 0.

division 0 resulting in double.positive_infinity.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -