for循环在arraylist中添加对象的某些字段

时间:2015-12-05 23:52:31

标签: java for-loop arraylist

很抱歉,标题不是很清楚。 无论如何,我正在制作一个垄断游戏,我目前正致力于所得税领域。我知道如何制作它,但我坚持的是一种应该获得所有金钱,财产等的总价值的方法。

这是我到目前为止所拥有的:

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运行,并且对于每个属性,将抵押值添加到变量“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;
    }
}

2 个答案:

答案 0 :(得分:4)

试试这个:

public void SendMailMessage(string FromAddress, string FromName, string ToName, string ToAddress, string Subject)
{
    MailAddress fromAdd = new MailAddress(FromAddress, FromName);
    MailAddress toAdd = new MailAddress(ToAddress);
    MailMessage msg = new MailMessage(fromAdd, toAdd);
    msg.IsBodyHtml = true;
    msg.BodyEncoding = System.Text.Encoding.UTF8;
    msg.Subject = Subject;
    string HtmlContent = "<div id='emailFormat' style='width: 100%; background-color: #efeac9;'>" +
                       "< div id = 'emailMsg' style = 'margin-left: auto; margin-right: auto; width: 600px; margin-top: 15px; font-family: Tahoma; font-size: .9em;' ><h2>Hello World</h2></div></div>");
    msg.Body = HtmlContent;
    SmtpClient client = new SmtpClient();
    client.Send(msg);
}

答案 1 :(得分:0)

循环遍历List的每个元素都是一种巧妙的方法:

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