提问人:DMC 提问时间:10/27/2022 更新时间:10/27/2022 访问量:35
每次添加玩家时,我都需要获取玩家数量
Every time I add a player I need to get the player count
问:
问题是当我添加一个新玩家时,每个添加的玩家的数量应该增加 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);
答:
1赞
sinclair
10/27/2022
#1
Player(String name){
this.name=name;
playerCount = playerCount + 1;
System.out.println("Player "+name+" added to the team!");
}
下一个:如何访问方法参数的变量?
评论
playerCount
从不增加