本帖最后由 yi_remember 于 2013-03-01 11:44:51 编辑

解决方案 »

  1.   

    new TestLoanClass().非静态变量
      

  2.   

    /*
     * 下列代码中的loanAmount显示错误。
     * Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
     * Cannot make a static reference to the non-static field loanAmount
     * 
     */
    TestLoanClass loan = new TestLoanClass(annualInterestRate, numberOfYears, loanAmount);
    /*
     *
     *
     *
      

  3.   


    改为
    import java.util.Date;
    import java.util.Scanner;
     
    public class TestLoanClass {
        
       private static double annualInterestRate;
       private static int numberOfYears;
       private static double loanAmount;
       private Date loanDate;
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter yearly interest rate, for example, 8.25: ");
            annualInterestRate = input.nextDouble();
             
            System.out.print("Enter number of yearr as an integer: ");
            numberOfYears = (int) input.nextDouble();
             
            /*
             * 下列代码中的loanAmount显示错误。
             * Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
             * Cannot make a static reference to the non-static field loanAmount
             * 
             */
            TestLoanClass loan = new TestLoanClass(annualInterestRate, numberOfYears, loanAmount);
             
            System.out.printf("The loan was created of %s\n" +
                "The monthly payment is %.2f\nThe total payment is %.2f\n", 
                loan.getLoanDate().toString(), loan.getMonthlyPayment(), loan.getTotalPayment());
            input.close();
             
        }     
        public TestLoanClass(){
            this(2.5, 1, 1000);
        }
        public TestLoanClass(double annualInterestRate, int numberOfYears, double loanAmount){
            this.annualInterestRate = annualInterestRate;
            this.numberOfYears = numberOfYears;
            this.loanAmount = loanAmount;
            loanDate = new Date();
        }
         
        public double getAnnualInterestRate(){
            return annualInterestRate;
        }
        public void setAnnualInterestRate(double annualInterestRate){
            this.annualInterestRate = annualInterestRate;
        }
        public int getNumberOfYears(){
            return numberOfYears;
        }
        public void setNumberOfYears(int numberOfYears){
            this.numberOfYears = numberOfYears;
        }
        public double getLoanAmount(){
            return loanAmount;
        }
        public void setLoanAmount(double loanAmount){
            this.loanAmount = loanAmount;
        }
         
        public double getMonthlyPayment(){
            double monthlyInterestRate = annualInterestRate /  1200;
            double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
            return monthlyPayment;
        }
        public double getTotalPayment(){
            double totalPayment = getMonthlyPayment() * numberOfYears * 12;
            return totalPayment;
        }
        public Date getLoanDate(){
            return loanDate;
        }
    }
      

  4.   

    TestLoanClass类 独立写到一个文件中
    import java.util.Date;public class TestLoanClass {
    private double annualInterestRate;
    private int numberOfYears;
    private double loanAmount;
    private Date loanDate;

    public TestLoanClass(){
    this(2.5, 1, 1000);
    }
    public TestLoanClass(double annualInterestRate, int numberOfYears, double loanAmount){
    this.annualInterestRate = annualInterestRate;
    this.numberOfYears = numberOfYears;
    this.loanAmount = loanAmount;
    loanDate = new Date();
    }

    public double getAnnualInterestRate(){
    return annualInterestRate;
    }
    public void setAnnualInterestRate(double annualInterestRate){
    this.annualInterestRate = annualInterestRate;
    }
    public int getNumberOfYears(){
    return numberOfYears;
    }
    public void setNumberOfYears(int numberOfYears){
    this.numberOfYears = numberOfYears;
    }
    public double getLoanAmount(){
    return loanAmount;
    }
    public void setLoanAmount(double loanAmount){
    this.loanAmount = loanAmount;
    }

    public double getMonthlyPayment(){
    double monthlyInterestRate = annualInterestRate /  1200;
    double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
    return monthlyPayment;
    }
    public double getTotalPayment(){
    double totalPayment = getMonthlyPayment() * numberOfYears * 12;
    return totalPayment;
    }
    public Date getLoanDate(){
    return loanDate;
    }}
    然后在public class TestLoanClass {    
        public static void main(String[] args)  测试
    不然就要把所有的方法编写成staitc的
      

  5.   

    出问题的地方:
    TestLoanClass loan = new TestLoanClass(annualInterestRate, numberOfYears, loanAmount);
    main()方法里没有loanAmount局部变量,会访问类成员变量,可是main()方法加载时,成员变量loanAmount还没有初始化,无法访问。
    可以修改成员变量为static成员变量,或者在main()方法里加入局部变量loanAmount。