Results 1 to 5 of 5
Thread: How to add loop statement
- 02-24-2013, 08:34 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 13
- Rep Power
- 0
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:
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:
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 + "."); } }
- 02-24-2013, 09:16 PM #2
- 02-24-2013, 09:18 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 13
- Rep Power
- 0
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.
- 02-24-2013, 09:56 PM #4
Senior Member
- Join Date
- Jan 2009
- Location
- CA, USA
- Posts
- 271
- Rep Power
- 10
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.
- 02-24-2013, 10:24 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
need help with if statement in loop
By Snackers in forum New To JavaReplies: 4Last Post: 01-11-2013, 07:04 PM -
repeat if statement in loop
By eng_hyzoom in forum New To JavaReplies: 5Last Post: 03-05-2012, 12:34 PM -
If Else Statement in While Loop
By rockintyler in forum New To JavaReplies: 3Last Post: 02-23-2012, 11:33 PM -
Help with loop statement
By arvind1508 in forum New To JavaReplies: 2Last Post: 02-23-2011, 04:39 PM -
Need help with a loop statement
By sunshine39 in forum New To JavaReplies: 7Last Post: 11-03-2008, 03:42 AM
Bookmarks