對不起,如果標題不是很清楚。無論如何,我正在製作一個壟斷遊戲,我正在研究收入所得稅空間。我對如何做一個想法,但什麼我卡上是應該得到所有的錢,物業總價值的方法等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;
}
}
添加'return Tot;',也許? –
你有拼寫錯誤。不是總而言之。 – bhspencer
是什麼讓你聲稱這是不正確的? –