-3

my loops problems seems to be running an not stopping am not getting the loop to stop it running cont and wont stop

rate=interest/100;
double monthly_rate=rate/period;
double n=period*length;
payment = (principal * Math.pow((1 + monthly_rate), n)) / n;

System.out.printf("Test acoount amount is %.2f",payment);

for(double i=payment; n<=n; n++){
    System.out.println(i+ "" +(payment-i));

}
Kara
  • 6,115
  • 16
  • 50
  • 57
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Feb 16 '18 at 17:25

2 Answers2

3
n <= n

will generally always be true.

You need to figure out the terminating condition of the loop, and possibly fix the n++ as well. It's likely to be something like:

for (int prd = 1; prd <= n; prd++) ...

which will loop n times with prd holding the values 1 through n inclusive.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • so what line should i put in there if i want it to stop at a certain amount – Kern Dennis Nov 19 '12 at 19:00
  • In his example, `n` is the amount that your loop will stop at as already stated in the answer, "which will loop `n` times with `prd` holding the values `1` through `n` inclusive." – Nick Hartung Nov 19 '12 at 19:21
1

Your problem is right there in the for statement itself:

for(double i=payment; n<=n; n++){

in the conditional n<=n

Basically your expression will never evaluate to anything other than what it's set too, because 'n will always equal n'

What you need is for your check to be a different variable or some kind of upper limit that you wish to cut things off at eg:

int max = 10;
for(double i=payment; n<=max; n++){

How you set and / or control max depends on exactly what your trying to achieve.

shawty
  • 5,729
  • 2
  • 37
  • 71
  • am trying to generate a loop based on what type of payment a person selects whether its monthly,semi-annually or annually – Kern Dennis Nov 19 '12 at 19:03
  • paxdiablos pretty much nailed it, what you need is something like: maxnumpayments = ; then something along the lines of the prd example above, with your counter going until prd <= maxnumpayments. Documentation for for loop is basically "for(initial variable; terminating condition; iteration condition) – shawty Nov 19 '12 at 22:13