我有一个名为 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];
}
我不断收到“不兼容类型”错误!
最佳答案
我看到与 max =playerList[i]
不兼容的类型,假设playerList是Player类型的数组。如何将一个玩家分配给一个双倍?除此之外,我还发现该方法还存在一些其他问题。
关于java - 如何创建一个包含比较元素以获得最高元素的数组的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9143825/