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

Java for循环在arraylist中添加对象的某些字段,java,for-loop,arraylist,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;

如果标题不是很清楚,我很抱歉。 不管怎样,我正在做一个垄断游戏,我目前正在研究所得税领域。我有一个想法如何做到这一点,但我坚持的是一个方法,应该得到所有的钱,财产等的总价值

以下是我目前掌握的情况:

public int getTotVal()
{
    int tot = 0;
    for (int i = 0; i < this.properties.size(); i++)
        tot += this.properties.get(i).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;
    }
}
试试这个:

public int getTotVal() {
    int tot = 0;
    for (int i = 0; i < this.properties.size(); i++)
        tot += this.properties.get(i).mortgage;
    return tot;
}
public int gettoval(){
int-tot=0;
对于(int i=0;i
试试这个:

public int getTotVal() {
    int tot = 0;
    for (int i = 0; i < this.properties.size(); i++)
        tot += this.properties.get(i).mortgage;
    return tot;
}
public int gettoval(){
int-tot=0;
对于(int i=0;i
有一种简单的方法可以循环列表中的每个元素:

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

有一种简单的方法可以循环列表中的每个元素:

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

增加一个
returntot,也许吧?你有拼写错误。Tot而不是Tot。是什么让你声称这是不对的?我本来打算最终这样做的,我还需要添加其他内容,但我想知道如何将抵押字段添加到for循环中,这是什么意思?我们需要看到更多的代码。你能显示所有的类吗?添加一个
returntot,也许吧?你有拼写错误。Tot而不是Tot。是什么让你声称这是不对的?我本来打算最终这样做的,我还需要添加其他内容,但我想知道如何将抵押字段添加到for循环中,这是什么意思?我们需要查看更多代码。您能显示所有类吗?@vlatkozelka我忘了返回,但我将Tot改为Tot,谢谢。@vlatkozelka我忘了返回,但我将Tot改为Tot,谢谢。“对象无法转换为属性”我明白了,我需要声明属性ArrayList是用于属性对象的“对象不能转换为属性”我明白了,我需要声明properties ArrayList用于属性对象