2015-12-05 41 views
0

對不起,如果標題不是很清楚。無論如何,我正在製作一個壟斷遊戲,我正在研究收入所得稅空間。我對如何做一個想法,但什麼我卡上是應該得到所有的錢,物業總價值的方法等for循環在陣列列表中添加對象的某些字段

這是我到目前爲止有:

public int getTotVal() 
{ 
    int tot = 0; 
    for (int i = 0; i < this.properties.size(); i++) 
     tot += this.properties.get(i).mortgage; 
    return tot; 
} 

for循環應該通過屬性的ArrayList運行,並且對於每個屬性,將抵押值添加到varialble「tot」。

我知道這是不對的,但我會如何正確地做到這一點?

編輯

球員:

import java.util.ArrayList; 


public class Player 
{ 
    private String name; 
    private String token; 
    public int wallet; 
    private ArrayList properties; 

    public Player(String name, String token, int wallet, Property prop) 
    { 
     this.name = name; 
     this.token = token; 
     this.wallet = wallet; 
     this.properties.add(prop); 
    } 

    /** 
    * @return the name 
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** 
    * @return the token 
    */ 
    public String getToken() { 
     return token; 
    } 

    /** 
    * @param token the token to set 
    */ 
    public void setToken(String token) { 
     this.token = token; 
    } 

    /** 
    * @return the wallet 
    */ 
    public int getWallet() { 
     return wallet; 
    } 



    /** 
    * @param wallet the wallet to set 
    */ 
    public void setWallet(int wallet) { 
     this.wallet = wallet; 
    } 

    /** 
    * @return the properties 
    */ 
    public ArrayList getProperties() { 
     return properties; 
    } 

    /** 
    * @param properties the properties to set 
    */ 
    public void setProperties(ArrayList properties) { 
     this.properties = properties; 
    } 

    //add buy() 
    //add butProp() 
    //add pay() 
    //add payRent() 

    public void pay(int amount) 
    { 
     this.wallet -= amount; 
    } 

    public int getTotVal() 
    { 
     int tot = 0; 
     for (Property property : this.properties) 
     { 
      tot += property.mortgage; 
     } 
     return tot; 
    } 
} 

物業:

package monopolysrc; 
import java.util.Scanner; 


public class Property extends Space 
{ 
    private int value; 
    private boolean owned; 
    private int mortgage; 

    public Property(String fn,String ln, int val, int mortgage, boolean owned) 
    { 
     super(fn,ln); 
     val = value; 
     owned = false; 
     this.mortgage = mortgage; 
    } 

    /** 
    * @return the value 
    */ 
    public int getValue() { 
    return value; 
    } 

    /** 
    * @param value the value to set 
    */ 
    public void setValue(int value) { 
     this.value = value; 
    } 

    /** 
    * @return the owned 
    */ 
    public boolean isOwned() { 
     return owned; 
    } 

    /** 
    * @param owned the owned to set 
    */ 
    public void setOwned(boolean owned) { 
     this.owned = owned; 
    } 

    /** 
    * @return the mortgage 
    */ 
    public int getMortgage() { 
     return mortgage; 
    } 

    /** 
    * @param mortgage the mortgage to set 
    */ 
    public void setMortgage(int mortgage) { 
     this.mortgage = mortgage; 
    } 
} 
+0

添加'return Tot;',也許? –

+0

你有拼寫錯誤。不是總而言之。 – bhspencer

+0

是什麼讓你聲稱這是不正確的? –

回答

0

有一種巧妙的方法遍歷列表中的每個元素:

for (Property property : this.properties) { 
    tot += property.mortgage; 
} 
+0

「對象不能轉換爲屬性」 –

+0

我想明白了,我需要說明屬性ArrayList是屬性對象 –

4

試試這個:

public int getTotVal() { 
    int tot = 0; 
    for (int i = 0; i < this.properties.size(); i++) 
     tot += this.properties.get(i).mortgage; 
    return tot; 
} 
+0

@vlatkozelka我忘了返回,但我改變TOT tot,謝謝。 –