使用从一个类到另一个类的方法

我的最后一种方法有问题。我被告知我必须在我的银行类的每月费用方法中使用我的 BankAccount.class 中的调整方法,但我无法弄清楚。我尝试了多种不同的方法,但无法使其正常工作。我们需要用月费调整账户余额。


Bank.java:33:错误:BankAccount 类中的方法调整不能应用于给定类型;BankAccount.adjust(); ^

必需:双重发现:无参数原因:实际参数列表和正式参数列表长度不同 1 错误


Bank.java:33: 错误:double 不能被取消引用 fee.adjust(); 1 个错误


Bank.java:33: 错误:找不到符号 bank.BankAccount.adjust(); 符号:变量 BankAccount 位置:BankAccount[] 类型的变量银行 1 错误


BankAccount.class


public class BankAccount {


String owner; // owner of account

int accountNumber; // integer account number

double balance = 0.0; // account balance

double amount; // adjusted amount to balance

String balanceFmt = "%.2f"; // string format for 2 decimal places


public BankAccount(String owner, int accountNumber) { //Constructor for the bank account

   this.owner = owner;

   this.accountNumber = accountNumber;

}


public double adjust(double amount) { //method to adjust balance

   this.balance += amount;

   return balance;

}

public String toString() { // method to print out account info

   return owner + " owns the account " + accountNumber + " with the balance of $" + String.format(balanceFmt,balance);


}

public double getBalance() { // method to get balance of accounts

   return balance;   

}

}

银行类


public class Bank {


BankAccount bank[];


public Bank() { // constructor for making a 10 account array

   bank = new BankAccount[10];

}


public void addAccount(BankAccount accounts) { // add account for giving numbers to accounts

   for(int i = 0; i < bank.length; i++) {

      if(bank[i] == null) {

         bank[i] = accounts;

         break;

      }

   }

}


BankAccount getAccount(int i) { //obtain account from BankAccount class

   return bank[i];

}


30秒到达战场
浏览 88回答 2
2回答

慕仙森

似乎您忘记将费用参数传递给 adjust() 方法。以下代码工作得很好&nbsp;public void monthlyFee(double fee) { //monthly fee for bank accounts&nbsp; &nbsp; for(int i = 0; i < bank.length; i++) {&nbsp; &nbsp; &nbsp; if(bank[i] != null) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(bank[i].adjust(fee));&nbsp; &nbsp; &nbsp; } //I have tried BankAccount.adjust() and couldn't work, bank[i].adjust() nothing seems to work&nbsp; &nbsp; }&nbsp; }创建银行1一个帐户 Joe Mac 拥有帐户 1234,余额为 $1000.00两个帐户 Joe Mac 拥有帐户 1234,余额为 $1000.00 Sally Ride 拥有帐户 2345,余额为 $2000.00三个帐户 Joe Mac 拥有帐户 1234,余额为 $1000.00 Sally Ride 拥有帐户 2345,余额为 $2000.00 Pat Armstrong 拥有帐户 3456,余额为 $3000.00月费 997.05 1997.05 2997.05 Joe Mac 拥有账户 1234,余额为 $997.05 Sally Ride 拥有账户 2345,余额为 $1997.05 Pat Armstrong 拥有账户 3456,余额为 $2997.05错误:没有发现错误!!!

MM们

很简单:当您检查您的测试代码时,您会发现在调用该adjust()方法时它传递了一个双精度值!您的其他代码(导致错误的代码)在调用该方法时似乎没有传递任何双精度!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java