我正在制作一个拼字游戏应用程序,我想制作一个系统,玩家可以在其中查看他们的分数以及姓名。分数应该从高到低排列。输出应如下所示: 名称 分数
Kathy 150
Charm 140
Albert 130
Arvin 120
Angelo 110
我正在尝试使用数组来做到这一点,但我很难做到这一点。
int score[]= new int[4];
score[0]= 120;
score[1]= 130;
score[2]= 140;
score[3]= 150;
String names[] = new String[4];
names[0]= "albert";
names[1]= "arvin";
names[2]= "charm";
names[3]= "kathy";
Player[] players = new Player[names.length];
for(int i = 0; i < names.length; i++) {
players[i] = new Player(names[i], score[i]);
show.setText((CharSequence) players[i]);
}
Arrays.sort(players, new PlayerComparator());
class PlayerComparator implements Comparator<Player> {
public int compare(Player p1, Player p2) {
return p1.getScore() < p2.getScore() ? -1
: p1.getScore() > p2.getScore() ? 1 : 0;
}
}
public class Player {
private String name;
private int score;
public Player(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
public String toString() {
return "name=" + name + "; score=" + score;
}
}
任何人都可以帮助我使用什么并指导我实现这个目标吗?非常感谢你。