I have this problem for homework and the issue I’m having is when I run the program and input the values I only get one result
for instance if I enter 30 for the number of years I get this “30 14730.576123040439” 30 times. Any ideas of what I can do to get the proper results to display(ie years 1-30 instead of just the correct result for year 30 30 times).
public class FinancialApp57
{
public static void main(String[] args)
{
//variables
double intRate, invAmt;
int yrs;
String inv = JOptionPane.showInputDialog("Enter an investment amount:");
invAmt = Double.parseDouble(inv);
String rate = JOptionPane.showInputDialog("Enter an intrest rate: ");
intRate = Double.parseDouble(rate);
String yr = JOptionPane.showInputDialog
("Enter the lenght of investment(in years): ");
yrs = Integer.parseInt(yr);
futureInvestmentValue(invAmt,intRate/12,yrs);
}
public static double futureInvestmentValue
(double investmentAmount,double monthlyIntrestRate,int years)
{
double FutureValue = 0;
int i = 1;
FutureValue=
investmentAmount*Math.pow((1+monthlyIntrestRate/100),years*12);
while(i<years)
{
System.out.println(years+"\t" + FutureValue);
i++;
}
return FutureValue;
}
}
the value your code prints is “years”, when you enter ’30’, then int years = 30, if you want something like (1,2,3,4,5….), you should print “i” instead of “years”, so must be: