2

我的一个学校项目有点问题。我们应该编写一个 Loan 类来执行与贷款相关的事情,例如返回每月付款和贷款的总付款。我的问题是我对此代码有特定的说明,我绝对不能超出这些说明。这是代码:

    import java.util.Scanner;
    import java.text.DecimalFormat;
    import java.lang.Math;

    public class Loan
    {
        public double annualInterestRate = 0;
        public int numberOfYears = 0;
        public double loanAmount = 0;

        public Loan()
        {
            annualInterestRate = 0.025;
            numberOfYears = 1;
            loanAmount = 1000;
        }

        public Loan(double interestRate, int numYears, double amount)
        {
            setRate(interestRate);
            setYears(numYears);
            setLoanAmount(amount);
        }

        public void setRate(double interest)
        {
        DecimalFormat percent = new DecimalFormat( "0.0%" );
            if(interest > 25 || interest < 0)
            {
                System.out.println("WARNING: Invalid annual interest rate: " + percent.format(interest) + ".");
                System.out.println("Current value not changed: " + percent.format(annualInterestRate * 100) + ".");
            }
            else
            {
                annualInterestRate = interest;
            }
        }

        public void setYears(int years)
        {
            if(years > 30 || years <= 0)
            {
                System.out.println("WARNING: Invalid number of years: " + years + ".");
                System.out.println("Current value not changed: " + numberOfYears + ".");
            }
            else
            {
                numberOfYears = years;
            }
        }

        public void setLoanAmount(double amnt)
        {
            DecimalFormat loan = new DecimalFormat( "$#,##0.00" );
            if(amnt <= 0)
            {
                System.out.println("WARNING: Invalid loan amount: " + loan.format(amnt) + ".");
                System.out.println("Current value not changed: " + loan.format(amnt) + ".");
            }
            else
            {
                loanAmount = amnt;
            }
        }

        public double getAnnualInterestRate()
        {
            return annualInterestRate;
        }

        public int getNumberOfYears()
        {
            return numberOfYears;
        }

        public double getLoanAmount()
        {
            return loanAmount;
        }

        public double getMonthlyPayment()
        {
            double monthly = annualInterestRate/12;
            double monthlyPayment = (loanAmount * monthly)/1 - (1/(1 + monthly));
            monthlyPayment = Math.pow(monthlyPayment, 12);
            return monthlyPayment;
        }

        public double getTotalPayment()
        {
            double totalPayment = getmonthlyPayment() * 12;
            return totalPayment;
        }

        public String toString()
        {
            DecimalFormat percent = new DecimalFormat( "0.0%" );
            DecimalFormat loan = new DecimalFormat( "$#,##0.00" );

            String interestRate = percent.format(annualInterestRate);
            String numOfYears = Integer.toString(numberOfYears);
            String loanAmnt = loan.format(loanAmount);

            String total = "Annual Interest Rate:\t" + interestRate + "\nNumber of Years:\t\t" + numOfYears + "\nLoan Amount:\t\t\t" + loanAmnt;

            return total;
        }
    }

我的问题是 getTotalPayment 方法。如果没有我将monthlyPayment 声明为一个字段(如annualInterestRate)或将其传递给getTotalPayment 方法,它就无法访问monthlyPayment 变量。问题是,getTotalPayment 不允许有参数,而且我们不允许有比她指示我们拥有的三个字段更多的字段,也就是您将在代码开头看到的三个字段。所以,我的问题是:有没有办法让 getTotalPayment 可以访问变量monthlyPayment,而不用让monthlyPayment 成为一个字段或给getTotalPayment 一个参数?

4

2 回答 2

1

您的getTotalPayment()方法中有拼写错误。

您尝试做的是getmonthlyPayment()在您应该调用getMonthlyPayment().

如果您错过了我的回答中的细微差别,当您想要大写的“M”时,您有一个小写的“m”。

我不完全确定这是否是您的问题,但它是我的 IDE 告诉我的唯一语法错误。

于 2015-11-06T17:09:12.610 回答
0

在您修改后的代码中,您需要大写M来调用getMonthlyPayment().

于 2015-11-06T17:02:29.717 回答