我必须用Java创建一个程序,该程序使用一种方法来计算未来的投资价值,用户可以投入投资金额和利率。它必须显示在表格中并具有年份1-30
。
我有一个错误,我无法弄清楚如何修复。我//Title
main
中的错误位于futureInvestmentValue
下。错误告诉我
futureInvestmentValue无法解析为变量
这是我的代码:
import java.util.Scanner;
public class futureInvestmentValue {
public static double futureInvestmentValue(double investmentAmount,
double monthlyInterestRate, int years){
double futureInvestmentValue = 1;
for (years = 1; years <= 30; years++) {
monthlyInterestRate = years/1200;
futureInvestmentValue =
investmentAmount * Math.pow((1+monthlyInterestRate), years*12);
}
return futureInvestmentValue;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//Get investment amount and yearly interest rate from user
Scanner input = new Scanner (System.in);
System.out.print("Enter investment amount, for example 100:" +
"Enter yearly interest rate, for example 5.25:\n");
double investmentAmount = input.nextDouble();
double annualInterestRate = input.nextDouble();
//Title
System.out.print("Years\t" + "\t Future Value");
for (int years = 1; years <= 30; years++) {
System.out.print(years);
System.out.printf("%4d", futureInvestmentValue);
}
System.out.println();
}
}
答案 0 :(得分:5)
在main方法中,您引用的是变量而不是函数名...
System.out.printf("%4d", futureInvestmentValue);
以上应该是:
System.out.printf("%4d", futureInvestmentValue(investmentAmount, ..... ));
顺便说一句,这就是为变量和函数赋予相同的名称。不要这样做。