如何比较三个整数值?

时间:2015-05-21 06:14:55

标签: java algorithm

我有三个整数值及其文本。我的要求是给所有人排名。

E.g。我有A = 50分,B = 500分,C = 50分。

现在我想比较所有这些并找到最大值和相等的值及其相应的名称(例如,A / B / C)。

编辑:: 作为输出它应该返回,B =第1级,A =第2级,C =第2级。

如果有人知道如何按照我的要求实现代码,那就太棒了。

提前致谢。

public class ScoreVO implements Comparator<Integer> {

    private String playerName = Constants.BLANK_STRING;
    private int playerScore;

    public String getPlayerName () {
        return playerName;
    }

    public void setPlayerName ( String playerName ) {
        this.playerName = playerName;
    }

    public int getPlayerScore () {
        return playerScore;
    }

    public void setPlayerScore ( int playerScore ) {
        this.playerScore = playerScore;
    }

    @Override
    public int compare ( Integer o1, Integer o2 ) {
        return o2.compareTo ( o1 );
    }
}

这是我的Comparator&lt;&gt;类。

如果我错了,请建议我。

4 个答案:

答案 0 :(得分:1)

一个示例运行代码,根据您的要求和玩家等级显示下面显示的输出。有一个单独的方法assignRank(List&lt;&gt;),您可以使用它为玩家分配排名。

得分列表:[ScoreVO [playerName = B,playerScore = 500,playerRank = 1],ScoreVO [playerName = A,playerScore = 50,playerRank = 2],ScoreVO [playerName = C,playerScore = 50,playerRank = 2 ]

public class ScoreExample {

    public static void main(String[] args) {
        List<ScoreVO> scoreList = new ArrayList<ScoreVO>();
        scoreList.add(new ScoreVO("A", 50));
        scoreList.add(new ScoreVO("C", 50));
        scoreList.add(new ScoreVO("B", 500));
        Collections.sort(scoreList);
        assignRank(scoreList);
        System.out.println("Score List: "+scoreList);
    }

    private static void assignRank(List<ScoreVO> scoreList) {
        int rank = 0;
        int score = 0;
        for(ScoreVO scoreVO : scoreList) {
            if(score != scoreVO.getPlayerScore()) {
                rank++;
                scoreVO.setPlayerRank(rank);
                score = scoreVO.getPlayerScore();
            } else {
                scoreVO.setPlayerRank(rank);
            }
        }
    }
}
class ScoreVO implements Comparable<ScoreVO> {
    public String playerName;
    public int playerScore;
    public int playerRank;

    public ScoreVO(String playerName, int playerScore) {
        this.playerName = playerName;
        this.playerScore = playerScore;
    }

    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public int getPlayerScore() {
        return playerScore;
    }

    public void setPlayerScore(int playerScore) {
        this.playerScore = playerScore;
    }

    public int getPlayerRank() {
        return playerRank;
    }

    public void setPlayerRank(int playerRank) {
        this.playerRank = playerRank;
    }

    @Override
    public int compareTo(ScoreVO o) {
        if(o.getPlayerScore() != getPlayerScore()) {
            if(getPlayerScore() > o.getPlayerScore())
                return -1;
            else
                return 1;
        } 
        return getPlayerName().compareTo(o.getPlayerName());
    }

    @Override
    public String toString() {
        return "ScoreVO [playerName=" + playerName + ", playerScore="
                + playerScore + ", playerRank=" + playerRank + "]";
    }
}

答案 1 :(得分:0)

ScoreVO应该实施Comparable<ScoreVO>。您的compareTo方法如下所示:

@Override
public int compareTo ( ScoreVO o ) {
    if(playerScore != o.playerScore)
       return Integer.compare(playerScore, o.playerScore);
    return playerName.compareTo(o.playerName);
}

答案 2 :(得分:0)

你应该使用Comparable来命令purpuses,使用equals()来计算公式(可以使用compareTo)

像这样

public class ScoreVO implements Comparable<ScoreVO> {

    @Override
    public int compareTo(ScoreVO other) {
        return other == null ? 1 : getPlayerScore() - other.getPlayerScore();
    }

    @Override
    public boolean equals(object other) {
        return !(other instanceof ScoreVO) ? false : compareTo(other) == 0 ;
    }
}

但是,您可能希望根据玩家名称比较相等性。想想将ScoreVO对象放在地图中 - 关键是什么?所以 -

    @Override
    public boolean equals(object other) {
        return other == null  ||  !(other instanceof ScoreVO) ? false :             
               getPlayerName.equals(other.getPlayerName()) ;
    }

答案 3 :(得分:0)

由于只有三个值,因此可以对所有操作进行硬编码。你可以想出一种非常紧凑和有效的方法来解决这个问题。

两个值的每次比较都可以得出结果>=<。为这些值分配值012,您可以使用base 3编码将三个比较打包在一个数字中。您最终会得到026范围内的数字,并且每个不同的值对应于您可以制表(或在switch语句中处理)的不同答案。

int Compare(int A, int B) { return A > B ? 0 : (A == B ? 1 : 2); }

char* Answer[27]= { "A1B2C3", "A1B1C3", "B1A2C3", ... }; // To be filled
return Answer[Compare(A, B) + 3 * compare(B, C) + 9 * compare(A, C)];