每次添加玩家时,我都需要获取玩家数量

Every time I add a player I need to get the player count

提问人:DMC 提问时间:10/27/2022 更新时间:10/27/2022 访问量:35

问:

问题是当我添加一个新玩家时,每个添加的玩家的数量应该增加 1。 当我尝试时,我只得到 0

public class Player {

    String name;
    static int playerCount=0;
    
    Player(String name){
        this.name=name;
        System.out.println("Player "+name+" added to the team!");
    }
            
    public static void main(String[] args) {
        
        Player one= new Player("x");
        System.out.println("the count now is "+ playerCount);
        
        Player two = new Player("y");
        System.out.println("the count now is "+ playerCount);
        
        Player three = new Player("z");
        System.out.println("the count now is "+ playerCount);
Java 构造函数 this

评论

1赞 sinclair 10/27/2022
playerCount从不增加
0赞 DMC 10/27/2022
没错,我不明白如何增加玩家数量。

答:

1赞 sinclair 10/27/2022 #1
 Player(String name){
        this.name=name;
        playerCount = playerCount + 1;
        System.out.println("Player "+name+" added to the team!");
    }