gpt4 book ai didi

java - 如何从 if 语句动态调用对象的引用名称?

转载 作者:太空宇宙 更新时间:2023-11-04 09:26:55 24 4
gpt4 key购买 nike

请原谅我的术语和对 Java 对象缺乏理解。我正在制作一个控制台大富翁游戏,其中包含一些类,大富翁,玩家,属性(property)等,其中大富翁有我的主要内容。我已经在 Monopoly 类中声明了 Property 和 Player 对象。玩家登陆购买的特性后,我尝试动态调用该特性的所有者,以从 Property 类将钱添加到该玩家的 .money 变量中。

我尝试使用 if 语句创建一个方法来返回一个 Player 对象,以及一个仅返回该属性所有者名称的字符串版本的方法。我的问题是我不知道如何使用它来实际用作对拥有它的玩家的引用。

属性类别

public class Property {


boolean available=false;
Object owner;
int rent = 0;
int houses = 0;
int hotels = 0;



}

玩家等级

public class Players{
Players(){
int money=1500;
}

String name="";
int money=0;
int pos=0;
int goCount=0;
int diceDoubles=0;
int rollsInJail=0;
boolean outOfJailFreeCard=false;
boolean jail=false;
String player="";

Scanner in=new Scanner(System.in);
String getName(){
name=(String) in.next();
return name;
}

}

职位类别

public class Position {


public static Players playerFind(Object str, Players obj){
if(str.equals(Monopoly.player1)){
return Monopoly.player1;
}
else if(str.equals(Monopoly.player2)){
return Monopoly.player2;
}
else if(str.equals(Monopoly.player3)){
return Monopoly.player3;
}
else if(str.equals(Monopoly.player4)){
return Monopoly.player4;
}
else if(str.equals(Monopoly.player5)){
return Monopoly.player5;
}
else if(str.equals(Monopoly.player6)){
return Monopoly.player6;
}
return obj;
}

public static void place(int num, Players obj, Property prop)
{
Scanner in=new Scanner(System.in);



if(num==1) {

System.out.println(obj.name+" landed on Mediterranean Avenue.");
if(Monopoly.mediterranean.available==true){
System.out.println("Would you like to buy this property?(Yes or No)");
String str = in.next();
if(str.startsWith("y") || str.startsWith("Y")){
Monopoly.mediterranean.available=false;
Monopoly.mediterranean.owner=obj;
obj.money-=60;
System.out.println("You now own Meditarreanean Avenue!");
System.out.println("Your balance is "+obj.money);
}
}
else{
Players obj2 = playerFind(Monopoly.mediterranean.owner, obj);
System.out.println("Rent is due! "+Monopoly.mediterranean.owner+" owns this property. M2 has been subtracted from your account"
+ " and added into "+Monopoly.mediterranean.owner+"'s bank roll.");
Players.obj2.money+=2;
System.out.println("Your balance is "+obj.money);

}
}
else if(num==2) {
System.out.println(obj.name+" landed on Community Chest.");


}//etc...

垄断级

public class Monopoly {
static Players player1 = new Players();
static Players player2 = new Players();
static Players player3 = new Players();
static Players player4 = new Players();
static Players player5 = new Players();
static Players player6 = new Players();
//static Position position= new Position();
static CommunityChest chest=new CommunityChest();
static Property property=new Property();
static Property mediterranean=new Property();
static Property baltic=new Property();
static Property reading=new Property();
static Property oriental=new Property();
static Property vermont=new Property();
static Property connecticut=new Property();
static Property stCharles=new Property();
static Property states=new Property();
static Property virginia=new Property();
static Property pennRail=new Property();
static Property stJames=new Property();
static Property tennessee=new Property();
static Property newYork=new Property();
static Property kentucky=new Property();
static Property indiana=new Property();
static Property illinois=new Property();
static Property bAndC=new Property();
static Property atlantic=new Property();
static Property ventor=new Property();
static Property marvin=new Property();
static Property pacific=new Property();
static Property northCar=new Property();
static Property pennAve=new Property();
static Property shortLine=new Property();
static Property parkPlace=new Property();
static Property board=new Property();
static Property electric=new Property();
static Property water=new Property();

static int playerNum=intro();

public static void play(int num, int rollCount){
int roll1=0, roll2, i=0;


while(i<rollCount-1){
int i1=i+1;
System.out.println("Beginning of turn "+i1);

if(num>0){
turn(player1);
System.out.println(player1.name+" money count:M"+player1.money);
}
if(num>1){
turn(player2);
System.out.println(player2.name+" money count:M"+player2.money);
}
if(num>2){
turn(player3);
}
if(num>3){
turn(player4);
}
if(num>4){
turn(player5);
}
if(num==6){
turn(player6);
}
System.out.println("End of turn "+i1+"\n");
i++;

}
}

public static void turn(Players obj){
Scanner in1=new Scanner(System.in);

int roll1=roll();
int roll2=roll();
obj.pos=obj.pos + roll1 + roll2;
System.out.println(obj.name+" rolled a "+roll1+" and a "+roll2);
if(obj.jail==false){
System.out.println(obj.name + " has landed on "+place(obj.pos));
}

if(roll1!=roll2){
obj.diceDoubles=0;
}

Position.place(obj.pos, obj, property);

if(obj.jail==true){//roll while in jail, check first in case they roll doubles and continue their turn. Turn must end after going to jail
if(obj.outOfJailFreeCard==true){//if get out of jail card, prompt to use it rightaways
System.out.println("You've rolled thrice doubles in a row and have ended in up jail. Luckily you have a 'Get out of jail free' card!");
System.out.print("Would you like to use your 'Get out of jail free' card right now?: (Y/N)");
String check=in1.next();
if(check.startsWith("y") || check.startsWith("Y")){
obj.pos=10;
obj.outOfJailFreeCard=false;
}
}
if(roll1!=roll2){
obj.rollsInJail+=1;
if(obj.rollsInJail==3){
obj.pos=10+roll1+roll2;
obj.money-=50;
obj.rollsInJail=0;
}return;//does this break out of method to end turn??
}
if(roll1==roll2){
obj.pos=10+roll1+roll2;
obj.rollsInJail=0;
}return;//does this break out of method to end turn??
}

while(roll1==roll2 && obj.diceDoubles<3){//check for doubles
obj.diceDoubles+=1;
if(obj.diceDoubles==3){
obj.jail=true;
obj.diceDoubles=0;
System.out.println("You've rolled doubles 3 times in a row. You are now in jail!");
break;
}
else{turn(obj);
break;}
}

if(obj.pos>39){//pass go, collect M200
obj.pos=obj.pos - 39;
obj.goCount=obj.goCount+1;
obj.money+=200;
}

in1.close();// close scanner
}

public static void main(String[] args) {

play(playerNum, 60);
}

我正在尝试动态查找所有者并引用实际对象实例以添加到其 .money 变量。

最佳答案

问题是您将owner作为Object引用,但它没有money字段。您需要使用 Player 类:

public class Property
{
boolean available=false;
Player owner;
int rent = 0;
int houses = 0;
int hotels = 0;
}

然后您可以直接在两个玩家之间转移租金,例如:

Property property = Monopoly.mediterranean;
activePlayer.money-= property.rent;
property.owner.money+= property.rent;

在真正的游戏中,您将添加更多检查,例如玩家是否有足够的钱来支付租金,或者是否存在需要解决的并行性问题,但这是问题的基本解决方案。

关于java - 如何从 if 语句动态调用对象的引用名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57567902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com