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;
return answer;
If anyone can help I would appreciate it.
InflationCalculator.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()); } }