-1

ArrayList の if 条件について質問があります。コードを参照してください:

public class Test {

    public static void main(String[] args) {

        ArrayList<BankAccount>accounts=new ArrayList<BankAccount>();
        BankAccount Mary=new BankAccount(1,50);
        BankAccount Lucy=new BankAccount(2,100);
        BankAccount Lily=new BankAccount(3,20);
        BankAccount Pete=new BankAccount(4,200);
        BankAccount Paul=new BankAccount(5,30);

        accounts.add(Mary);
        accounts.add(Lucy);
        accounts.add(Lily);
        accounts.add(Pete);
        accounts.add(Paul);

        double min = accounts.get(0).getBalance();
        int poorestPerson = accounts.get(0).getAccountNumber();
        for(BankAccount a:accounts) {
            if(a.getBalance()<min) {
                poorestPerson=a.getAccountNumber();
            }
        }
        System.out.println("Poorest person is "+poorestPerson);
    }
}

結果は常に 5 ですが、次を追加すると、正しい出力は 3 になります。

  poorestPerson=a.getAccountNumber();
  **min=a.getBalance();** 

それは正しい答えを出力します、私の質問は、このコマンドが結果をどのように変えるかです?乾杯.

このコマンドがどのように機能するかは理解しましたが、min=a.getBalance(); と書いていない場合は誰か説明してください。、条件はどのように機能し、最後の要素を出力するのはなぜですか?

4

4 に答える 4