1
我有一个名为Player类包括以下实例变量:如何创建一个方法包含一个数组来比较元素以获得最高的一个?
private String name, position;
private int numGames, numGoalsScored, numGoalsConceded;
类也有两个构造函数,一个有名字和位置参数,另一种是拷贝构造函数。 我被要求创建一个名为Team
的类,该类应该包含许多方法来控制Player
类。其中一种方法是bestPlayer()
,这种方法应该让玩家获得最好的表现,计算方法是在玩家类中写入的getPerformance();
方法。
我的问题是:如何创建一个方法来获得最佳球员?
我写了下面的代码,但遗憾的是它不工作:
/* Get the best player */
public Player bestPlayer()
{
if (playerNum == 0)
{
System.out.println("Error: Empty team!");
}
double max = 0;
int index = 0;
for (int i = 0; i<playerNum; i++){
if(playerList[i].getPerformance() > max) {
max = playerList[i];
}
}
return playerList[index];
}
我不断收到的是“不兼容的类型”错误!