2016-04-20 104 views
0

我有,我可以訪問使用球員的集合:爪哇 - 玩家收集 - 計算勝/負

public Collection<Player> getAllPlayers() { 
    return players.values(); 
} 

我需要找到使用player.getResult()結果最高的球員。然後使用player.setWon()更新所有其他玩家。如果玩家有相同的結果(平局),我需要setWon(false)並且不改變他們的總分數。輸球的球員需要從他們的總額中減去他們的投注,並且贏得的球員我需要將他們的投注加入他們的總額中。

在平局中掙扎,我如何區分一名球員獲勝,並獲得相同的總勝數? 這是我到目前爲止有:

//for every player in collection 
    for (Player player : getAllPlayers()) 
    { 
    //find the largest player result 
    int resultToTest = player.getResult(); 
    if (resultToTest > bestResult) { 
     bestResult = resultToTest; 
    } 
    else if (resultToTest == bestResult){ 
     player.setWon(false);   
    } 
    else if(resultToTest < bestResult){ 
     player.setWon(false); 
    } 
    } 
    for (Player player : getAllPlayers()) 
    { 
    //set win for player with largest result 
    resultToTest = player.getResult(); 
    if (resultToTest == bestResult) { 
     player.setWon(true); 
     player.setPoints(player.getPoints() + player.getBet()); 
    } 
    else if (resultToTest < bestResult){ 
     player.setPoints(player.getPoints() - player.getBet()); 
    } 

回答

0

這將是更好的使用索引:

final List<Player> players = getAllPlayers(); // or Player[] or Collection<Player> 
int besti = 0; 
int bestpts = players.get(0); 

for (int i=1; i<players.size(); i++) { 
    final int curpts = players.get(i).getResult(); 
    if (curpts > bestpts) { 
     bestpts = curpts; 
     besti = i; 
    } 
} 

for (int i=0; i<players.size(); i++) 
    players.get(i).setWon(i == besti);