Can't get loop to work


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Can't get loop to work

  1. #1
    Join Date
    Mar 2005
    Posts
    1

    Can't get loop to work

    Hello,
    Can anyone advise me on why I can't get the following code to loop? I'm trying to get continuous calculation until the variable amount = 0.
    I will work out display params after I get the loop to work. I'm new to Java, but not coding. This is a simple program so there's probably a simple answer.
    My thanks to anyone willing to teach me.
    Thanks
    DW


    Code:
    import java.text.*;
    
    class Wk3
    {
        public static void main (String[] arguments)    //create main
        {
            int term = 30;                              //init var for length of loan
            double amount = 200000;                        //init var for loan amt
            double pmt = 0;                             //init var for payment
            double rate = .0575;                        //init var for interest rate
            double rateMo = 0;                          //init var for monthly interest paid        
            {
                rate = (rate/12);                       //divide annual rate to get monthly rate
                term = (term * 12);                     //multiply years to get length in months            
                pmt= (amount * (rate)) / (1-Math.pow(1 + rate, - term));    //compute payment
                {
                    if (amount > 0)                         //run if counter >0
                    {
                        java.text.DecimalFormat dec = new java.text.DecimalFormat(",###.00");               //format next output to dollars.cents
                        System.out.println("\nThis months payment is $" + dec.format (pmt));                //display payment
                        System.out.println ("\nYour loan balance is $" + dec.format (amount));              //display loan amt
                        System.out.println("\nThis months interest is $" + dec.format (amount * (rate )));  //montly interest payment
                    }
                    amount -=  pmt;
                }
            }
        }
    }

  2. #2
    Join Date
    Mar 2005
    Posts
    15
    i can see your problem already. it's not a loop. you put everything in the main method and it all should just run once. to get it to run multiple times put one of the type of loops (do...while, while, for) inside the main function with the coding inside the loop.

    ex:

    Code:
    import java.text.*;
    
    class Wk3
    {
        public static void main (String[] arguments)    //create main
        {
            int term = 30;                              //init var for length of loan
            double amount = 200000;                        //init var for loan amt
            double pmt = 0;                             //init var for payment
            double rate = .0575;                        //init var for interest rate
            double rateMo = 0;                          //init var for monthly interest paid     
            while (amount != 0) //beginning of while loop
            {
                rate = (rate/12);                       //divide annual rate to get monthly rate
                term = (term * 12);                     //multiply years to get length in months            
                pmt= (amount * (rate)) / (1-Math.pow(1 + rate, - term));    //compute payment
                {
                    if (amount > 0)                         //run if counter >0
                    {
                        java.text.DecimalFormat dec = new java.text.DecimalFormat(",###.00");               //format next output to dollars.cents
                        System.out.println("\nThis months payment is $" + dec.format (pmt));                //display payment
                        System.out.println ("\nYour loan balance is $" + dec.format (amount));              //display loan amt
                        System.out.println("\nThis months interest is $" + dec.format (amount * (rate )));  //montly interest payment
                    }
                    amount -=  pmt;
                }//end of while loop
            }
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center