# Need help with loop

• February 6th, 2011, 06:29 PM
ciwas01
Need help with loop
Hello, I am having trouble with my loop. I need to write a code that executes as followed:

Enter the current cost of the item: 1000

Enter the expected inflation rate per year
(Enter as a percentage and do not include the percent sign): 6

Enter the whole number of years in the future to project cost: 10

At 6.00% inflation per year, the cost in 10 year(s) will be \$1,790.85.

Process completed.

My loop looks like this:
for(numberOfYears = 1; numberOfYears >= 1; numberOfYears++)
answer = (cost * rateOfInflation) + cost;

If anyone can help I would appreciate it.

InflationCalculator.java

Code java:

```public class InflationCalculator { private int numberOfYears; private double cost, rateOfInflation, answer, next;   public InflationCalculator(double initialCost, int initialNumberOfYears, double initialRateOfInflation){ cost = initialCost; numberOfYears = initialNumberOfYears; rateOfInflation = initialRateOfInflation; }   public void set(double newCost, int newNumberOfYears, double newRateOfInflation){ cost = newCost; numberOfYears = newNumberOfYears; rateOfInflation = newRateOfInflation *.01; }   public double getAnswer(){ for(numberOfYears = 1; numberOfYears >= 1; numberOfYears++) answer = (cost * rateOfInflation) + cost; return answer;   }   public InflationCalculator(){ cost = 0; numberOfYears = 0; rateOfInflation = 0; }   }   RunInflationCalculator.java   import java.util.Scanner;   public class RunInflationCalculator {   public static void main(String[] args){   Scanner keyboard = new Scanner (System.in);   InflationCalculator calcObject = new InflationCalculator(1.2,1,1.12);   System.out.print("Enter the current cost of the item:"); double cost = keyboard.nextInt();   System.out.println("Enter the expected inflation rate per year"); System.out.print("(Enter as a percentage and do not include the percent sign:)"); double rateOfInflation = (keyboard.nextDouble());   System.out.print("Enter the whole number of years in the future to project cost:"); int numberOfYears = keyboard.nextInt();   calcObject.set(cost, numberOfYears, rateOfInflation);   System.out.println(calcObject.getAnswer()); } }```
• February 7th, 2011, 12:50 AM
DanBrown
Re: Need help with loop
Quote:

for(numberOfYears = 1; numberOfYears >= 1; numberOfYears++)
how many times this will iterate according to you.
• February 7th, 2011, 07:50 AM
KevinWorkman
Re: Need help with loop
When posting code, make sure you use the code or highlight tags. I've edited your post to show you how to use them.