宿題にこの問題があり、プログラムを実行して値を入力すると、たとえば、この「30 14730.576123040439」を取得する年数に 30 を入力すると、1 つの結果しか得られないという問題が 30 回発生します。適切な結果を表示するために私ができることのアイデア(つまり、30年目の正しい結果を30回ではなく、1年から30年)。
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;
}
}