System.out.println不打印变量

时间:2014-01-30 01:44:11

标签: java

我收到此错误消息

finalAmount cannot be resolved to a variable

这是有问题的代码

public class DepositCalculator {

    public static final double annualInterestRate = 1.0;
    public static final int numberOfYears = 5;
    public static final int monthsPerYear = 12;
    public static final int deposit = 1000;
    public static final int depositAmountIncrement = 1000;

    public static void main(String[] args) {

        System.out.println(finalAmount);
    }

    public static double calculateAmountNoCompounding(double deposit, double annualInterestRate, int numberOfYears) {

        double finalAmount = deposit*(1+annualInterestRate*numberOfYears);
        return finalAmount;
    }     
}

2 个答案:

答案 0 :(得分:2)

您需要调用方法

  

System.out.println(calculateAmountNoCompounding(deposit,annualInterestRate,numberOfYears));

答案 1 :(得分:1)

您尝试从另一个方法访问变量方法局部变量。检查该变量的范围。方法局部变量可以在只声明它们的方法中访问。

finalAmount方法中定义的

calculateAmountNoCompounding(),因此,它只有一个范围包含该方法。您需要打印从calculateAmountNoCompounding()返回的值,而不是方法局部变量finalAmount