我在这里确实是一个非常容易和快速的问题......所以我想说我的账户类如下:
import java.text.NumberFormat;
public class Account
{
private final double RATE = 0.03; // interest rate of 3.5%
private long acctNumber;
private double balance;
private String name;
//-----------------------------------------------------------------
// Sets up the account by defining its owner, account number,
// and initial balance.
//-----------------------------------------------------------------
public Account (String owner, long account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
//-----------------------------------------------------------------
// Deposits the specified amount into the account. Returns the
// new balance.
//-----------------------------------------------------------------
public double deposit (double amount)
{
balance = balance + amount;
return balance;
}
//-----------------------------------------------------------------
// Withdraws the specified amount from the account and applies
// the fee. Returns the new balance.
//-----------------------------------------------------------------
public double withdraw (double amount, double fee)
{
balance = balance - amount - fee;
return balance;
}
//-----------------------------------------------------------------
// Adds interest to the account and returns the new balance.
//-----------------------------------------------------------------
public double addInterest ()
{
balance += (balance * RATE);
return balance;
}
//-----------------------------------------------------------------
// Returns the current balance of the account.
//-----------------------------------------------------------------
public double getBalance ()
{
return balance;
}
//-----------------------------------------------------------------
// Returns a one-line description of the account as a string.
//-----------------------------------------------------------------
public String toString ()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return acctNumber + "\t" + name + "\t" + fmt.format(balance);
}
}
我创建了这里显示的Bank类......
public class Bank
{
Account[] accounts;// = new Account[30];
int count=0;
String name;
public Bank(String name)
{
this.name = name;
accounts = new Account[30];
}
public void addAccount(Account acct)
{
accounts[count] = acct;
count++;
}
public void addInterest()
{
//for (Account acct : accounts)
//acct.addInterest();
for(int i = 0; i < count; i++)
accounts[i].addInterest();
}
}
如果我尝试使用addInterest()方法,我会收到错误 对于(帐户帐户:帐户)循环,您会看到已注释掉。有人可以告诉我这是为什么吗?我认为这些循环是等价的。提前谢谢。
答案 0 :(得分:1)
可迭代数组上的for循环遍历所有30个元素,而不仅仅是你真正添加的元素。
您可以使用ArrayList<Account>
并根据需要添加元素。这允许您省略计数字段:
public class Bank
{
ArrayList<Account> accounts = new ArrayList<Account>();
String name;
public Bank(String name)
{
this.name = name;
}
public void addAccount(Account acct)
{
accounts.add(acct);
}
public void addInterest()
{
for (Account acct : accounts)
acct.addInterest();
}
}
答案 1 :(得分:0)
您必须初始化帐户数组
因此您可能希望将其更改为:
public void addInterest()
{
//for (Account acct : accounts)
//acct.addInterest();
for(int i = 0; i < count; i++)
accounts[i].addInterest();
}
这样的事情:
public void addInterest()
{
for (Account acct : accounts) {
acct= new Account("John",1234596069,200.00);
acct.addInterest();
}
// for(int i = 0; i < count; i++)
// accounts[i].addInterest();
}
基本上,您必须在调用方法之前初始化数组变量。