# paying off dept ( can't make it stop at 0 )

• September 16th, 2010, 03:51 AM
bossmeister
paying off dept ( can't make it stop at 0 )
Heey,

I'm having a problem with this code. It doesn't want to stop at 0.

Code Java:

```public class Main {   /** * @param args the command line arguments */ public static void main(String[] args) { double schuld = 100000; double rp = 9.0; double rente; double af = 15000; double sum; int jaar = 1;   sum = 0; rente = (schuld/100) * rp;   System.out.println("Jaar Schuld Rp Rente Aflossing"); System.out.println(jaar + " " + schuld + " " + rp + " " + rente + " " + af);     while (schuld >= 0){   rente = (schuld/100) * rp; schuld = (schuld + rente) - af; jaar++; System.out.println(jaar + " " + schuld + " " + rp + " " + rente + " " + af); if(schuld == 0) schuld = sum; } } }```

• September 16th, 2010, 04:09 AM
Brt93yoda
Re: paying off dept ( can't make it stop at 0 )
Quote:

I'm having a problem with this code. It doesn't want to stop at 0.
Well... Your while loop says >= 0. Either change your loop to
Code java:

`while (schuld > 0){`

or change this line
Code java:

`sum = -1;`
• September 16th, 2010, 07:00 AM
aussiemcgr
Re: paying off dept ( can't make it stop at 0 )
Quote:

paying off dept ( can't make it stop at 0 )
Isn't that more like real life though?

Seriously though, Brty93yoda is correct. The case you specify in the while loop is the case in which it will leave. This is important because the case is evaluated BEFORE it runs the loop (unlike a Do-While). So when schuld is equal to 0 and you are expecting it to do the loop 1 more time, it actually meets the condition of schuld>=0 and it leaves the loop right then.
• September 16th, 2010, 07:33 AM
j2me64
Re: paying off dept ( can't make it stop at 0 )
hello bossmeister, your while-loop runs only until the variable schuld is >= 0 and then stops. the schuld variable is calculated with the formula (schuld + rente) - af. in order to become 0, you must adapt your formulara arguments or the formula, otherwise the debitor will overpay his debt. i think this is not a java but a financial issue.