我必须在Java中创建一个程序,该程序使用一种方法来计算未来投资价值,同时用户输入投资金额和利率。它必须在表格中显示并且有1-30
年。错误:futureInvestmentValue无法解析为变量
我有一个错误,我无法弄清楚如何解决。错误在//Title
的main
之下,我有futureInvestmentValue
。错误是告诉我,
futureInvestmentValue cannot be resolved to a variable
这里是我的代码:
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();
}
}
您在'futureInvestmentValue()'中声明了该变量,因此您只能在该方法中访问该变量。如果你想在'main()'中访问,你需要声明它是一个静态的类变量。另一个选择是在最后使用'()'在打印语句中调用该方法。 – csmckelvey 2014-10-01 22:01:46
是的,因为它不是一个变量,但你可以这样使用它。 – njzk2 2014-10-01 22:02:00