【发布时间】:2017-03-14 07:14:18
【问题描述】:
我有一个方法需要将提供的银行帐户添加到我创建的数组中:
public boolean addAccount (BankAccount newAccount[]) {
if (numAccounts == 0) {
return false;
}
else {
return true;
for(int counter=0; counter<newAccount.length; counter++)
newAccount[counter] += accounts;
}
}
用这个方法测试:
public static boolean test5() {
System.out.println("Test5: add an account to a customer.");
BankAccount b = new BankAccount();
Customer c1 = new Customer("Alice", "Smith");
customerCounter ++;
if (!c1.addAccount(b))
return false;
return c1.toString().equals("Alice Smith, " + c1.getCustomerID() + "\n" + b.toString() + "\n");
}
但是我收到一个错误,eclipse 在这一行中没有解决方案:
newAccount[counter] += accounts;
【问题讨论】:
-
编译器应该给你一些关于无法访问代码的警告(
for在return之后) -
未收到无法访问代码的错误。我已将其修改为 this.newAccount[counter]+=accounts;现在我收到一条错误消息,说 newAccount 不是一个字段。
-
Oleg Estekhin,如果数组初始化为
null
标签: java arrays methods constructor