Java 帐户类工作不正常
我犯了一个错误 帐户ID为:0 账户余额为:0.0 帐户ID是:1122 账户余额为20500.0 主线程java.lang中出现异常。错误:未解决的编译问题: 类型帐户中的getMontlyInterestRatedouble方法不适用于参数Java 帐户类工作不正常,java,class,Java,Class,我犯了一个错误 帐户ID为:0 账户余额为:0.0 帐户ID是:1122 账户余额为20500.0 主线程java.lang中出现异常。错误:未解决的编译问题: 类型帐户中的getMontlyInterestRatedouble方法不适用于参数 public class AccountDriver { public static void main(String[] args) { // ID, Balance, Annual Interest Rate
public class AccountDriver {
public static void main(String[] args) {
// ID, Balance, Annual Interest Rate
Account number1 = new Account();
Account number2 = new Account(1122, 20000.00, 0.045);
// Default account
System.out.println("The Account ID is: " + number1.getId());
System.out.println("The Account Balance is: " + number1.getBalance());
// System.out.println("The Account Balance is: "+
// number1.getMontlyInterest());
System.out.println("");
// Ask to withdraw 2500
System.out.println("The Account ID is: " + number2.getId());
number2.withdraw(2500.00);
number2.deposit(3000.00);
System.out.println("Account Balance is " + number2.getBalance());
// System.out.println("The montly interest is : "+
// number2.getMontlyInterest());
System.out.println("");
}
}
public class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
public Account(int id, double balance, double annualInterestRate) {
this.setId(id);
this.setBalance(this.balance);
this.setBalance(annualInterestRate);
}
public Account() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMontlyInterest(double montlyInterest) {
// Given Formula
// double MontlyInterest= this.balance * get.MontlyInterestRate();
return montlyInterest;
}
public double getMontlyInterestRate(double montlyInterestRate) {
// Given Formula
montlyInterestRate = this.annualInterestRate / 12;
return montlyInterestRate;
}
double withdraw(double amount) {
return balance -= amount;
}
double deposit(double amount) {
return balance += amount;
}
}
您在代码中犯了两个小错误 在构造函数中,这两行
at Account.getMontlyInterest(Account.java:41)
at AccountDriver.main(AccountDriver.java:21)
应该是
this.setBalance(this.balance); // this.balance is the instance variable and not the parameter passed
^^^^ - this is not required, just use the balance parameter passed.
this.setBalance(annualInterestRate); // you are re-writing the balance with interest rate
^^^^^^^^^^ - You need to set annual interest rate and not the balance here.
现在,由于设置了AnnualInterestate,您可以通过如下修改GetMontlyInterestate方法来获得月利率
this.setBalance(balance); // sets the balance passed to the instance variable balance
this.setAnnualInterestRate(annualInterestRate); // sets the annual interest rate
您可以通过取消System.out.println代码的注释来打印您的月利率
月利息法如下所示:
System.out.println("The montly interest is : "+ number2.getMontlyInterestRate());
你为什么叫这一点;在帐户构造函数中两次,传递annualInterestate?哦,应该是this.setBalancethis.annualInterestate;对吧?不!应该是这样的;之前的setBalance调用应该是这个。setBalancebalance;-请注意,参数的前缀不是这个!montly interest法呢?@RJ你可能需要指出这一点。平衡,因为它不太明显……我明白了,谢谢。那么月利率呢,我放//这样程序就可以编译它了月利率很好问题是月利率method@user3304653-像这样说话对你没有好处。检查我所做的编辑并尝试它们。不要在这里大喊大叫,因为每个人都在这里帮助你。所有大写字母都意味着对别人大喊大叫,我也可以这么做,但我不想。哦,对不起,我不知道
System.out.println("The montly interest is : "+ number2.getMontlyInterestRate());
public double getMontlyInterest() { // no parameter required
// Given Formula
double MontlyInterest = this.balance * getMontlyInterestRate(); // balance multiplied by monthly interest rate
return MontlyInterest; // return the value
}
System.out.println("The montly interest is : "+ number2.getMontlyInterest());