java如何创建一个包含数组的方法来比较元素以获得最高值?
我有一个名为Player的类,它由以下实例变量组成:
private String name, position;
private int numGames, numGoalsScored, numGoalsConceded;
该类还有两个构造函数,一个以name和position作为参数,另一个是复制构造函数。
我被要求创建一个名为Team
的类,这个类应该包含许多方法来控制Player
类。其中一个方法是bestPlayer()
,这个方法应该得到性能最好的播放器,它是由在player类中编写的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];
}
我一直在犯“不兼容类型”的错误
# 1 楼答案
我看到一个与
max = playerList[i]
不兼容的类型,假设playerList是类型Player的数组。你怎么能把一个球员分配给替补队员?除此之外,我还看到了该方法的一些其他问题