好的,所以我有两个类:一个用于玩家,一个用于属性。
public class Player {
int money = 1500;
Object NamePlayer;
String Names;
ArrayList properties = new ArrayList(15);
public Player(String Name) {
Names = Name;
}
public void recieve(int ammount){
money = money + ammount;
}
public void payment(int ammount){
money = money - ammount;
}
public void addProperty(Property name){
if (name.Value > money){
System.out.println("Sorry, You dont have enough money!");
}else{
money -= name.Value;
properties.add(name.Name);
name.setOwner(this, name);
}
}
和属性类
public class Property {
String Name;
int Value;
int House1Value;
int House2Value;
int House3Value;
int House4Value;
int Hotel1Value;
int Hotel2Value;
int Hotel3Value;
int Hotel4Value;
int Houses;
int Hotels;
int Rent;
Object Owner;
public Property(int value, int houses, int hotel, int rent,
int house1,int house2,int house3,int house4, int hotel1,
int hotel2,int hotel3,int hotel4, String names){
Value = value;
Houses = houses;
Hotels = hotel;
Rent = rent;
House1Value = house1;
House2Value = house2;
House3Value= house3;
House4Value= house4;
Hotel1Value= hotel1;
Hotel2Value= hotel2;
Hotel3Value= hotel3;
Hotel4Value= hotel4;
Name = names;
}
public int addHotel(int numHotel){
if (Hotels < 4){
Hotels += numHotel;
return Hotels;
}else{
System.out.println("Sorry, You must Purchase more Houses to do that.");
}
return 2;
}
public int addHouse(int numHouse){
if (Houses < 4){
Houses += numHouse;
}else{
Houses = Houses;
}
return Houses;
}
public int determineRent(int numHouse, int numHotel){
if (numHotel == 4){
Rent += Hotel4Value;
}else if (numHotel ==3){
Rent += Hotel3Value;
}else if (numHotel == 2){
Rent += Hotel2Value;
}else if (numHotel == 1){
Rent += Hotel1Value;
}else if (numHouse == 4){
Rent += House4Value;
}else if (numHouse == 3){
Rent += House3Value;
}else if (numHouse == 2){
Rent += House2Value;
}else{
Rent = Rent + House1Value;
}
return Rent;
}
public void setOwner(Player owner, Property property){
Owner = owner.Names;
然后我的主Java文件看起来像这样
public class Monopoly {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Player Bob = new Player("Bob");
Player John = new Player("John");
Property Boardwalk = new Property(400,0,0,400,600,0,0,0,0,0,0,1000,"Boardwalk");
showMoney(Bob);
showMoney(John);
Bob.addProperty(Boardwalk);
System.out.println(Boardwalk.Owner);
showRent(Boardwalk);
payRent(John, Bob, Boardwalk);
showMoney(John);
showMoney(Bob);
showRent(Boardwalk);
}
public static void showMoney(Player player){
System.out.println(player.Names + " 's current balance is $" + player.money);
}
public static void showProperties(Player player){
System.out.println(player.Names + " owns " + player.properties);
}
public static void showRent(Property property){
System.out.println(property.determineRent(property.Houses, property.Hotels));
}
public static void payRent(Player player1, Player player2, Property property){
if(property.Owner == player2.Names){
player1.payment(property.determineRent(property.Houses, property.Hotels));
player2.recieve(property.determineRent(property.Houses, property.Hotels));
}else{
System.out.println("Sorry " + player2.Names + " does not own " + property.Name + "!");
}
}
问题是。为什么当我运行这个程序时,我得到以下内容:
鲍勃目前的余额为1500美元 约翰目前的余额是1500美元 短发 1000 约翰目前的余额为-100美元 鲍勃目前的余额为3300美元 2800当我将相同的号码传递给每个玩家各自的方法时?