如何在没有参数和参数的方法之间传递变量

How to pass a variable between methods without parameters and arguments

我的一个学校项目有点问题。我们应该编写一个贷款 class 来处理与贷款相关的事情,例如 return 每月还款额和贷款的总还款额。我的问题是我有关于这段代码的具体说明,我绝对不能超出。 这是代码:

    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 一个参数?

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

您的 getTotalPayment() 方法存在拼写错误。

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

时调用方法 getmonthlyPayment()

万一你错过了我的答案中的细微差别,你有一个小写 'm' 而你想要一个大写 'M'.

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