抱歉,如果标题不是很清楚。 不管怎样,我正在制作一个垄断游戏,目前正在研究所得税领域。我知道如何做到这一点,但我坚持的是一种应该获得所有金钱、属性(property)等的总值(value)的方法。
这是我到目前为止所拥有的:
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;
}
}
最佳答案
试试这个:
public int getTotVal() {
int tot = 0;
for (int i = 0; i < this.properties.size(); i++)
tot += this.properties.get(i).mortgage;
return tot;
}
关于java - for 循环将对象的某些字段添加到数组列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34112335/