2013-12-13 68 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(); 
}