对于Loan类型,方法Loan(double, int, double)没有定义



谁能帮我看看我在哪里错过了这个?我试图让贷款类调用到主方法,并检查我的异常处理是否正确。确切地说,我是第6周才开始做这个,我可以利用所有可能的建设性帮助。提前感谢!

package loan;
import java.util.Scanner;
public class Loan {
public static void main(String[] args) {
    try {
        Loan(2.5, 0, 1000.00);
    }
    catch (IllegalArgumentException ex) {
        ex.getMessage();
    }
}
Scanner input = new Scanner(System.in);
double annualInterestRate;
int numberOfYears;
double loanAmount;
private java.util.Date loanDate;
public Loan() {
    this(2.5, 0, 1000);
}
public Loan(double annualInterestRate, int numberOfYears, double loanAmount) {
    this.annualInterestRate = annualInterestRate;
    this.numberOfYears = numberOfYears;
    this.loanAmount = loanAmount;
    loanDate = new java.util.Date();
}

public double getAnnualInterestRate() {
    if (annualInterestRate > 0)
        return annualInterestRate;
    else
        throw new IllegalArgumentException("Interest rate cannot be zero or negative.");
}
public void setAnnualInterestRate(double annualInterestRate) {
    this.annualInterestRate = annualInterestRate;
}
public int getNumberOfYears() {
    if (numberOfYears > 0)
        return numberOfYears;
    else
        throw new IllegalArgumentException("Number of years cannot be zero");
}
public void setNumberOfYears(int numberOfYears) {
    this.numberOfYears = numberOfYears;
}
public double getLoanAmount() {
    if (loanAmount > 0)
        return loanAmount;
    else
        throw new IllegalArgumentException("Loan amount cannot be zero");
}
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 java.util.Date getLoanDate() {
    return loanDate;
}
}

不清楚您期望在main中做什么:

try {
    Loan(2.5, 0, 1000.00);
}

我怀疑你的意思是一个构造函数调用:

try {
    new Loan(2.5, 0, 1000.00);
}

你的帖子显示了一些术语上的混淆,这可能导致了这个问题:

我试图让贷款类调用到主方法

你不调用一个类。调用构造函数或方法。你的意思是:

我正试图使main方法调用Loan类的构造函数。

此时,您可能更清楚地知道需要new

您需要首先使用"new"关键字创建一个Loan对象。这将调用构造函数。

Loan loanVar = new Loan(2.5, 0, 1000.00);

您的代码尝试调用一个名为Loan的静态方法,但是看起来您想要创建一个新的Loan对象。您可以使用关键字new并为构造函数提供正确的参数:

// create a new Loan using the no-args constructor
Loan defaultLoan = new Loan();
// or create a new Load with the specified rate/duration/amount
Loan myLoan = new Loan(2.5, 0, 1000.0);

要在主方法中调用构造函数,需要使用new关键字。就像在Loan构造函数中初始化loanDate一样。

最新更新