0

ここで非常に簡単で簡単な質問だと確信しているものがあります...次のようなアカウントクラスがあるとしましょう:

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();
    }
}

コメントアウトされている for (Account acct: accounts) ループで addInterest() メソッドを使用しようとすると、エラーが発生します。誰かが私にこれがなぜなのかについての洞察を提供してもらえますか? これらのループは同等だと思いました。前もって感謝します。

4

2 に答える 2

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();
    }
}
于 2012-07-23T00:35:34.657 に答える
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();
        }

基本的に、メソッドを呼び出す前に配列変数を初期化する必要があります。

于 2012-07-23T00:31:46.100 に答える