【问题标题】:for loop to add certain fields of an object in an arraylistfor循环在arraylist中添加对象的某些字段
【发布时间】:2015-12-05 23:52:31
【问题描述】:

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

这是我目前所拥有的:

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;
    }
}

【问题讨论】:

  • 加个return Tot;,也许吧?
  • 您有拼写错误。 Tot 而不是 tot。
  • 是什么让你声称它不正确?
  • 我最终会这样做,我还需要添加其他内容,但我想知道如何在 for 循环中将抵押字段添加在一起
  • 你是什么意思?我们需要查看更多代码。你能显示所有的类吗?

标签: java for-loop arraylist


【解决方案1】:

试试这个:

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

【讨论】:

  • @vlatkozelka 我忘了返回,但我把 Tot 改成了 tot,谢谢。
【解决方案2】:

有一种巧妙的方法可以遍历 List 的每个元素:

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

【讨论】:

  • "对象不能转换为属性"
  • 我想通了,我需要声明属性 ArrayList 是用于属性对象的
猜你喜欢
  • 1970-01-01
  • 2018-03-15
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
  • 2012-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多