gpt4 book ai didi

java - Loan 类型的方法 Loan(double, int, double) 未定义?

转载 作者:行者123 更新时间:2023-12-01 07:38:42 24 4
gpt4 key购买 nike

谁能帮我看看我在哪里错过了这个?我试图将贷款类调用到主方法,并检查我的异常处理是否正确。我对此非常陌生,确切地说是第六周,并且可以使用所有可能的建设性帮助。提前致谢!

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);
}

您的帖子表明术语有些困惑,这可能导致了此问题:

I am trying to get the loan class called to the main method

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

I am trying to make the main method call the constructor for the Loan class.

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

关于java - Loan 类型的方法 Loan(double, int, double) 未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8245216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com