Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By AndrewM16921

Thread: How to add loop statement

  1. #1
    Hawks is offline Member
    Join Date
    Feb 2013
    Posts
    13
    Rep Power
    0

    Default How to add loop statement

    Hi everyone,

    I am in need of finishing up my problem. Before I ask my questions here is the problem:

    Write a method that computes future investment value at a given interest rate for a specified number of years.
    Use the following method header:

    How to add loop statement-screen-shot-2013-02-24-2.32.46-pm.png

    Write a test program that prompts the user to enter the investment amount (e.g., 1000) and the interest rate (e.g., 9%) and prints a table
    that displays future value for the years from 1 to 30, as shown below:
    How to add loop statement-screen-shot-2013-02-24-2.27.27-pm.png

    I need to create a loop to show years (1-30), as I already have gotten the other codes. Here is what I have so far.

    Java Code:
    public static void main(String[] args) {
            
            // Step 0: create a Scanner for input
            Scanner input = new Scanner(System.in);
            
            // Step 1: enter investment amount
            System.out.print("Enter investment amount: ");
            double investmentAmount = input.nextDouble();
            
            // Step 2: enter annual interest rate
            System.out.println("Enter annual interest Rate, e.g. 4.25: ");
            double annualInterestRate = input.nextDouble();
                    
            // Step 3: calculate monthly rate from annual interest rate
            double monthlyInterestRate = annualInterestRate / 1200 ;
                    
            // Step 4: enter number of years
            System.out.print(
                    "Enter number of years as an integer, for example 1: ");
            int numberofYears = input.nextInt();
            
            // Step 5: compute future value
            double futureInvestmentValue = investmentAmount * Math.pow(1 + (annualInterestRate * .01
                    /12),numberofYears * 12); 
            
            // Step 6: output future value
            System.out.println("Future value with interest compunded monthly:" + "$"
                    + futureInvestmentValue + ".");
            
        }
    }
    Thanks guys!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: How to add loop statement

    Quote Originally Posted by Hawks View Post
    Before I ask my questions ...
    Did you forget to ask your questions?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Hawks is offline Member
    Join Date
    Feb 2013
    Posts
    13
    Rep Power
    0

    Default Re: How to add loop statement

    Sorry if I was confusing. My question is how would I start/make the loop to display the 30 future value results.

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: How to add loop statement

    First you'd want some method to calculate the future value for a single year. Then, you loop from 0 to 30 and call that method as needed. Something like this:

    Java Code:
    public static void main(String[] args)
    {
    	double[] amount = new double[30];
    	double[] annual = new double[30];
    	double[] monthly = new double[30];
    	//fill data from input...
    	
    	System.out.println("Years \t\t Future Value");
    	for(int i = 0; i < 30; i++)
    	{
    		System.out.println(i + " \t\t " + futureValue(amount[i], annual[i], monthly[i], i));
    	}
    }
    
    public static double futureValue(double amount, double annual, double monthly, int years)
    {
    	double value = 0.0;
    	// calculate future value based on data
    	return value;
    }
    Last edited by AndrewM16921; 02-24-2013 at 09:58 PM.
    Hawks likes this.

  5. #5
    Hawks is offline Member
    Join Date
    Feb 2013
    Posts
    13
    Rep Power
    0

Similar Threads

  1. need help with if statement in loop
    By Snackers in forum New To Java
    Replies: 4
    Last Post: 01-11-2013, 07:04 PM
  2. repeat if statement in loop
    By eng_hyzoom in forum New To Java
    Replies: 5
    Last Post: 03-05-2012, 12:34 PM
  3. If Else Statement in While Loop
    By rockintyler in forum New To Java
    Replies: 3
    Last Post: 02-23-2012, 11:33 PM
  4. Help with loop statement
    By arvind1508 in forum New To Java
    Replies: 2
    Last Post: 02-23-2011, 04:39 PM
  5. Need help with a loop statement
    By sunshine39 in forum New To Java
    Replies: 7
    Last Post: 11-03-2008, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •