[英]The method Loan(double, int, double) is undefined for the type Loan?
Can anyone help me see where I am missing this? 谁能帮我看看我在想什么吗? I am trying to get the loan class called to the main method and check to see if my exception handling is right. 我正在尝试调用main方法的贷款类,并检查我的异常处理是否正确。 I am very new to this, week six to be exact, and can use all the constructive help possible. 我对此很陌生,确切地说是第六周,可以使用所有可能的建设性帮助。 Thanks ahead of time! 提前谢谢!
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;
}
}
It's not clear what you expect this to do in main
: 目前尚不清楚您对main
期望是什么:
try {
Loan(2.5, 0, 1000.00);
}
I suspect you meant it to be a constructor call: 我怀疑您是说它是构造函数调用:
try {
new Loan(2.5, 0, 1000.00);
}
Your post gives an indication of a bit of confusion over terminology, which may have led to this problem: 您的帖子表明您对术语有些困惑,这可能导致了此问题:
I am trying to get the loan class called to the main method 我试图让贷款类被称为main方法
You don't call a class. 你不叫一类。 You call a constructor or a method. 您调用构造函数或方法。 What you meant is: 您的意思是:
I am trying to make the
main
method call the constructor for theLoan
class. 我试图使main
方法调用Loan
类的构造函数。
At that point, it's potentially clearer that you need new
. 到那时,您可能更清楚需要new
。
You need to create a Loan object first with the "new" keyword. 您首先需要使用“ new”关键字创建一个Loan对象。 This will call the constructor. 这将调用构造函数。
Loan loanVar = new Loan(2.5, 0, 1000.00);
To invoke a constructor in your main method you need to use the new keyword. 要在您的main方法中调用构造函数,您需要使用new关键字。 Like you do for initializing loanDate in the Loan constructor. 就像您在Loan构造函数中初始化loanDate一样。
Your code tries to call a static method called Loan, but it looks like you want to create a new Loan object. 您的代码尝试调用一个名为Loan的静态方法,但是您似乎想创建一个新的Loan对象。 You do that by using the keyword new and giving the correct parameters to the constructor: 您可以通过使用关键字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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.