我是Java的新手(C ++和C是我常用的语言)
我试图弄清楚如何对一组对象进行排序。我需要让用户输入有多少玩家,玩家姓名和玩家分数。然后程序将从顶部到底部输出得分和球员名称。
我让用户输入他们的信息,并将其存储到播放器类。
我无法弄清楚如何对玩家对象分数进行排序。我很确定我需要使用类比,但对于我的生活,我无法弄清楚如何设置它。
有人可以帮忙吗?
我知道播放器类中的代码不正确lol
import java.util.*;
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Scanner input1 = new Scanner(System.in);
int allPlayers;
int index[] = new int[12];
int i= 0;
System.out.print("Please enter the number of players");
allPlayers = input.nextInt();
Player[] playersArray = new Player[allPlayers];
for(i = 0; i <allPlayers; i++){
playersArray[i] = new Player();
System.out.print("What is the name of Player # " + (i+1) +"?");
playersArray[i].name = input1.nextLine();
System.out.print("What was the score of Player # " + (i+1) + "?");
playersArray[i].score = input.nextInt();
}
System.out.print(playersArray[i].name);
System.out.print(playersArray[i].score);
}
}
public class Player implements Comparable<Player> {
private int score; // players score
private String name; // players name
public Player(int score, String name){
this.core = score;
this.name = name;
}
public int compareTo(Player other){
int last = this.score.compareTo(other.score);
return last == 0 ? this.name.compareTo(other.score) : score;
}
}
答案 0 :(得分:1)
代码中的问题是对compareTo()
方法的误解。
如果第一个参数(在本例中,this
被认为是第一个参数)更大,则方法返回-1,如果它们相等则返回0,如果第二个参数更大,则返回1。
public int compareTo(Player other){
if (other == null){
return -1; // If the other is null, it goes last
}
// First, compare by the score:
int scoreComparison = Integer.compare(this.score, other.score);
// That's good enough, unless the scores are equal
if (scoreComparison != 0){ // then they're not equal, so we know our answer
return scoreComparison;
} else { // The scores are equal, so compare the names and return the result
if (this.name == null){ // Equal if both null, otherwise non-null wins
return other.name == null ? 0 : 1;
} else {
return this.name.compareTo(other.name);
}
}
}
答案 1 :(得分:0)
蛋糕走路。做了一些简化和清理。
class Player implements Comparable<Player> {
public final int score; // players score
public final String name; // players name
public Player(final int score, final String name) {
this.score = score;
this.name = name;
}
@Override public int compareTo(final Player other) {
return other.score - this.score;
// return this.score - other.score; // or this to reverse order
}
}
public class PlayerSorting {
public static void main(final String[] args) {
try (final Scanner input = new Scanner(System.in);//
final Scanner input1 = new Scanner(System.in);) {
System.out.print("Please enter the number of players");
final int allPlayers = input.nextInt();
final Player[] playersArray = new Player[allPlayers];
for (int i = 0; i < allPlayers; i++) {
System.out.print("What is the name of Player # " + (i + 1) + "?");
final String name = input1.nextLine();
System.out.print("What was the score of Player # " + (i + 1) + "?");
final int score = input.nextInt();
playersArray[i] = new Player(score, name);
}
// sort
Arrays.sort(playersArray);
// output all
System.out.println("SCORES:");
for (final Player player : playersArray) {
System.out.println("\t" + player.name + "\t" + player.score);
}
}
}
}