2013-12-13 67 views
1
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)的值。

我会说它的小事我做错了,但如果任何人都可以为我指出,那会很好。

+0

'System.out.println(“Total Points:”+ total);' – Anugoonj

回答

2
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; 
+1

哈我现在觉得很傻,估计今天我不会用它。感谢您的帮助Prabhakaran – user2757842

+0

@ user2757842欢迎您。 – Prabhakaran

+1

@ user2757842将它标记为答案,如果你觉得它有帮助 –

2

可以使用的foreach而不是为:

for(Object t:vec) 
{ 
    total += ((Team)t).getPoints(); 
}