我在使用最后一种方法时遇到麻烦。我被告知,我必须在我的bank类的monthFee方法中使用BankAccount.class中的adjust方法,但无法弄清楚。我尝试了多种不同的方法,但无法正常工作。我们需要用月费调整帐户余额。
Bank.java:33:错误:BankAccount类中的方法Adjust不能为 适用于给定的类型; 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;
}
}
Bank.class
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];
}
public void printAccounts() { //prints out account statuses
for(int i = 0; i < bank.length; i++) {
if(bank[i] != null) {
System.out.println(bank[i]);
}
}
}
public void monthlyFee(double fee) { //monthly fee for bank accounts
for(int i = 0; i < bank.length; i++) {
if(bank[i] != null) {
} //I have tried BankAccount.adjust() and couldn't work, bank[i].adjust() nothing seems to work
}
}
}
BankTest.class
public class BankTest {
/*
* test - set up a bank and add accounts
*/
public static void main(String[] args) {
// Code to test Bank and BankAccount classes
int errors = 0;
double fee = -2.95;
Assignment assignment = new Assignment();
assignment.homework("Homework 2a");
System.out.println("\nCreate bank1");
Bank bank1 = new Bank();
System.out.println("\nOne account");
BankAccount bankAccount1 = new BankAccount("Joe Mac", 1234);
bankAccount1.adjust(1000.0);
bank1.addAccount(bankAccount1);
bank1.printAccounts();
System.out.println("\nTwo accounts");
BankAccount bankAccount2 = new BankAccount("Sally Ride", 2345);
bankAccount2.adjust(2000.0);
bank1.addAccount(bankAccount2);
bank1.printAccounts();
System.out.println("\nThree accounts");
BankAccount bankAccount3 = new BankAccount("Pat Armstrong", 3456);
bankAccount3.adjust(3000.0);
bank1.addAccount(bankAccount3);
bank1.printAccounts();
System.out.println("\nMonthly Fee");
bank1.monthlyFee(fee);
bank1.printAccounts();
System.out.println("\nErrors:");
if (bank1.getAccount(0).getBalance() != 997.05) {
errors += 1;
System.out.println("Balance for account at index 0 does not match $997.05");
}
if (bank1.getAccount(1).getBalance() != 1997.05)
{
errors += 1;
System.out.println("Balance for account at index 1 does not match $1997.05");
}
if (bank1.getAccount(2).getBalance() != 2997.05)
{
errors += 1;
System.out.println("Balance for account at index 2 does not match $2997.05");
}
if (errors == 0)
System.out.println("No errors found!!!");
}
}
非常感谢您的帮助和指导。谢谢。
答案 0 :(得分:1)
简单:检查测试代码时,发现调用 adjust()方法时,它传递一个双精度值!
您的其他代码(导致错误的代码)在调用该方法时似乎未传递任何双精度值!
答案 1 :(得分:1)
似乎您忘记了传递费用参数来调整()方法。以下代码可以很好地工作
public void monthlyFee(double fee) { //monthly fee for bank accounts
for(int i = 0; i < bank.length; i++) {
if(bank[i] != null) {
System.out.println(bank[i].adjust(fee));
} //I have tried BankAccount.adjust() and couldn't work, bank[i].adjust() nothing seems to work
}
}
创建bank1
一个帐户 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
错误: