public static void main(String[] args)
    {
        Vector vec = new Vector();

        vec.add(new Team(1, "Manchester City", 38, 64, 89));
        vec.add(new Team(2, "Manchester United", 38, 56, 89));
        vec.add(new Team(3, "Arsenal", 38, 25, 70));
        vec.add(new Team(4, "Tottenham", 38, 25, 69));
        vec.add(new Team(5, "Newcastle", 38, 5, 65));

        int points = 0;
        int total = 0;

        for(int i = 0; i < vec.size(); i++)
        {
            points = ((Team) vec.elementAt(i)).getPoints();
            total += points;
        }
        System.out.println("Total Points: " + points);


    }


谁能在这里帮助我,我要做的就是将对象中最后一个参数的所有值加在一起。

我下面的内容只是打印出最后一个对象的值(65)。

我会说这是我做错的小事,但是如果有人可以为我指出,那将很棒。

最佳答案

 System.out.println("Total Points: " + points);
                                        |
                                        look here it should be total.


像这样改变。

 System.out.println("Total Points: " + total);


要么

更改

points = ((Team) vec.elementAt(i)).getPoints();
total += points;




points += ((Team) vec.elementAt(i)).getPoints();
total = points;

关于java - 通过 vector 相加,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20567800/

10-09 04:58