Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help with loop

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;
    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());	
    	}
    }
    Last edited by KevinWorkman; February 7th, 2011 at 08:50 AM. Reason: added highlight tags


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Need help with loop

    for(numberOfYears = 1; numberOfYears >= 1; numberOfYears++)
    how many times this will iterate according to you.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  2. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM