Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Can't get loop to work

Posts: 3Member
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("
This months payment is \$" + dec.format (pmt)); //display payment
System.out.println ("
Your loan balance is \$" + dec.format (amount)); //display loan amt
System.out.println("
This months interest is \$" + dec.format (amount * (rate ))); //montly interest payment
}
amount -= pmt;
}
}
}
}
· ·

• Posts: 684Member
· ·
• Posts: 31Member
Change the if statement into a while loop

[code] if (amount > 0) //run if counter >0 [/code]

should look like:

[code] while(amount > 0) [/code]

: 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("
This months payment is \$" + dec.format (pmt)); //display payment
: System.out.println ("
Your loan balance is \$" + dec.format (amount)); //display loan amt
: System.out.println("
This months interest is \$" + dec.format (amount * (rate ))); //montly interest payment
: }
: amount -= pmt;
: }
: }
: }
: }
:

· ·
• Posts: 684Member
: Change the if statement into a while loop
:
: [code] if (amount > 0) //run if counter >0 [/code]
:
: should look like:
:
: [code] while(amount > 0) [/code]
:
: : 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("
This months payment is \$" + dec.format (pmt)); //display payment
: : System.out.println ("
Your loan balance is \$" + dec.format (amount)); //display loan amt
: : System.out.println("
This 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
· ·