コードを更新しましたが、実行時に出力されるテーブルの下部に、年を初期化した数値が引き続き表示されます。そして、私はまだ、印刷セクションの%.2dを使用して2桁の小数点以下を作成する方法を理解しようとしています。ですから、それについてのフィードバックも役に立ちます。
import java.util.Scanner;
public class Investment {
public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years){
double futureInvestmentValue = 0.0;
for (years = 1; years <=30; years++){
//calculate futureInvestmentValue
futureInvestmentValue += (investmentAmount * (Math.pow(1 + monthlyInterestRate, years * 12)));
System.out.print(years + "\t" + futureInvestmentValue + "\n");
}//end for
return futureInvestmentValue;
}//end futureInvestmentValue
public static void main(String[] args){
Scanner input = new Scanner (System.in);
//obtain Investment amount
System.out.print("Enter Investment amount: ");
double investmentAmount = input.nextDouble();
//obtain monthly interest rate in percentage
System.out.print("Enter annual interest rate in percentage: ");
double annualInterestRate = input.nextDouble();
//calculate annual interest rate
double monthlyInterestRate = (annualInterestRate / 1200);
int years = 1;
System.out.println("Years\t" + "Future Value");
System.out.print(years + "\t" + futureInvestmentValue(investmentAmount, monthlyInterestRate, years) + "\n");
}//end main
}//