0

假设我有一个 Player 类,例如:

public class Player {

String name;
int chips;
int betVal;

}

以下代码对于创建玩家数组是否正确?

public static void main(String[] args) {
    int playerCount;
    int startingChip;
    out.print("How many players? ");
    playerCount = myScanner.nextInt();
    Player[] aPlayer = new Player[playerCount + 1];

    for (int i = 0; i < playerCount + 1; i++){
        aPlayer[i] = new Player();
    }

    out.print("Enter starting chip amount: ");
    startingChip = myScanner.nextInt();

}

如果是这样,我将如何为每个玩家分配姓名、筹码金额和下注值?以后如何在代码中访问和更改它们?

编辑:将播放器作为对象或数组(名称,筹码,betVal)以便以后访问它会更容易吗?

4

4 回答 4

2

首先,您的变量需要是私有的(有一部分程序员更喜欢公共变量,但大多数人更喜欢私有)。

您可以通过重载的构造函数或通过 setter 设置 Player 对象的值。

public class Player {


    private String name;
    private int chips;
    private int betVal;


    public Player(){
    //default constructor to initialize without any parameters
    }

    public Player(String name, int chips, int betVal){
        this.name=name;
        this.chips=chips;
        this.betVal=betVal;

    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChips() {
        return chips;
    }

    public void setChips(int chips) {
        this.chips = chips;
    }

    public int getBetVal() {
        return betVal;
    }

    public void setBetVal(int betVal) {
        this.betVal = betVal;
    }
}

在这种情况下,您的初始化可能是

aPlayer[i] = new Player("Jason", 5,1000);

或使用设置器,如

for (int i = 0; i < playerCount + 1; i++){
        aPlayer[i] = new Player();
 }


 aPlayer[i].setName("Jason");
 aPlayer[i].setChips(5);
 aPlayer[i].setBetVal(1000);

考虑到您的示例程序,我猜选项 2 效果很好。

于 2012-10-30T15:44:05.787 回答
1

为什么不在创建任何东西之前询问所有用户的输入?举个例子 :

public static void main(String[] args) {
    int playerCount;
    int startingChip;

    out.print("How many players? ");
    playerCount = myScanner.nextInt();
    out.print("Enter starting chip amount: ");
    startingChip = myScanner.nextInt();

    Player[] aPlayer = new Player[playerCount];

    for (int i = 0; i < playerCount; i++){
        aPlayer[i] = new Player();
        aPlayer[i].setChips(startingChip);
    }
}

您还应该使用列表。最后,如果您想询问每个玩家的姓名,请直接在循环中执行:

for (int i = 0; i < playerCount; i++){
    aPlayer[i] = new Player();
    aPlayer[i].setChips(startingChip);

    out.print("What's the player " + i + " name? ");
    aPlayer[i].setName(myScanner.next());
}
于 2012-10-30T15:40:38.140 回答
1

您可以通过告诉数组您要访问哪个播放器来访问每个播放器。

`aPlayer[0].name = "JAG";`

例如会工作。

于 2012-10-30T15:40:12.403 回答
0

尝试这样的事情......在循环本身中询问个人玩家信息。

    System.out.println("How many players? ");
    Scanner myScanner = new Scanner(System.in);;
    playerCount = myScanner.nextInt();
    Player[] aPlayer = new Player[playerCount];

    for (int i = 0; i < playerCount; i++){
        aPlayer[i] = new Player();

        System.out.println("Enter Name for Player " + i+1);
        String name = myScanner.next();

        System.out.println("Enter chips for Player " + i+1);
        int chips = myScanner.nextInt();

        System.out.println("Enter betVal for Player " + i+1);
        int betVal = myScanner.nextInt();

        aPlayer[i].name = name;
        aPlayer[i].chips = chips;
        aPlayer[i].betVal = betVal;
    }

还将 Player 类的实例变量设为私有,并使用 getter 和 setter 访问它们。

于 2012-10-31T10:55:11.387 回答