Why is this giving me an infinite loop?

Posted by Chase Yuan on Stack Overflow See other posts from Stack Overflow or by Chase Yuan
Published on 2012-10-07T02:57:37Z Indexed on 2012/10/07 3:38 UTC
Read the original article Hit count: 131

Filed under:

I was going through a code used to calculate investments until it has doubled and I received an infinite loop that I can't seem to solve. Can anyone figure out why this is giving me an infinite loop? I've gone through myself but I can't seem to find the problem. The "period" referred is how many times per year the interest is compounded.

double account = 0; //declares the variables to be used
   double base = 0;
   double interest = 0;
   double rate = 0;
   double result = 0;
   double times = 0;
   int years = 0;
   int j;

   System.out.println("This is a program that calculates interest.");
   Scanner kbReader = new Scanner(System.in); //enters in all data  
   System.out.print("Enter account balance: ");
   account = kbReader.nextDouble();
   System.out.print("Enter interest rate (as decimal): ");
   rate = kbReader.nextDouble();

   System.out.println("                    " + "Years to double" + "     " +     "Ending balance");
   base = account;
   result = account;
   for (j=0; j<3; j++){
       System.out.print("Enter period: ");
       times = kbReader.nextDouble();
       while (account < base*2){
           interest = account * rate / times;
           account = interest + base;
           years++;
       }
        account = (((int)(account * 100))/100.0);
       //results
       System.out.print("                         " + i + "               " + account + "\n");
       account = result;
   }

The code should ask for three "periods", or three different times the entered data is compounded per year (ex annually, monthly, daily etc.)

Thanks a lot!

© Stack Overflow or respective owner

Related posts about java