Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Can't get loop to work Posted by rockybalboa on 20 Mar 2005 at 3:01 PM
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

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;
}
}
}
}
Report
Re: Can't get loop to work Posted by Vilanye on 20 Mar 2005 at 6:50 PM
Where is your loop statement?
Report
Re: Can't get loop to work Posted by dehade on 21 Mar 2005 at 4:58 AM
Change the if statement into a while loop

 if (amount > 0) //run if counter >0 


should look like:

 while(amount > 0) 


: 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
:
: 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;
: }
: }
: }
: }
:

Report
Re: Can't get loop to work Posted by Vilanye on 21 Mar 2005 at 9:31 AM
: Change the if statement into a while loop
:
:
 if (amount > 0) //run if counter >0 

:
: should look like:
:
:
 while(amount > 0) 

:
: : 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
: :
: : 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;
: : }
: : }
: : }
: : }
: :
:
:


That would be an infinite loop as this line: amount -= pmt; is outside the if block



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.