我正在用Java做一个小游戏,用于学校目的。
现在,我要计算播放器的数量,代码如下:
Player player1;
player1 = new Player();
player1.name = "Name1";
player1.score = 0;
player1.lives = 100.0;
Player player2;
player2 = new Player();
player2.name = "Name2";
player2.score = 0;
player2.lives = 50.0;
最佳答案
在播放器中,使用static variable计数在播放器构造函数中递增。
private static int playerCount = 0;
//constructor
Player(){
playerCount++;
}