Java 根据总值对值组进行排序
我正在编写一个用于评估比赛分数和排名的应用程序,我有一个具体的问题: 我有团队,每个团队由4名参赛者组成。每个队员都有一些罚分,然后对整个队伍进行总结。例如:Java 根据总值对值组进行排序,java,sorting,Java,Sorting,我正在编写一个用于评估比赛分数和排名的应用程序,我有一个具体的问题: 我有团队,每个团队由4名参赛者组成。每个队员都有一些罚分,然后对整个队伍进行总结。例如: Team1 name, discipline1, discipline2, discipline3, total, RANK first xxxxxxxx, 10 , 20 , 30 , 60 , 2 second xxxxxxxx, 10 , 2
Team1 name, discipline1, discipline2, discipline3, total, RANK
first xxxxxxxx, 10 , 20 , 30 , 60 , 2
second xxxxxxxx, 10 , 20 , 30 , 60 , 2
third xxxxxxxx, 10 , 20 , 30 , 60 , 2
fourth xxxxxxxx, 10 , 20 , 30 , 60 , 2
total 40 , 80 , 120 , 240
Team2 name, discipline1, discipline2, discipline3, total, RANK
first xxxxxxxx, 10 , 10 , 30 , 50 , 1
second xxxxxxxx, 10 , 10 , 30 , 50 , 1
third xxxxxxxx, 10 , 10 , 30 , 50 , 1
fourth xxxxxxxx, 10 , 10 , 30 , 50 , 1
total 40 , 40 , 120 , 200
等等
我需要根据total/total单元格中的总罚分对这些团队进行排序。如何有效地解决这个问题?这是我在java中的第一个应用程序,因此我将感谢任何帮助 您应该使用列表来存储团队类实例,这样您就可以轻松地对它们进行排序。寻找比较器和可比较接口的示例。Team类将包含teamMembers,它存储带有惩罚的TeamMember类实例,并且TeamMember应该具有惩罚总和的getter方法。此方法将从团队实例调用,以计算团队惩罚的总和。因此,在您的比较中,您将能够调用team method,它返回团队成员惩罚的总和。只要我不知道团队或竞争对手是否会拥有比示例数据更多的数据,这是您的第一个
Java
应用程序之一,我建议您首先使用简单对象来解决此问题
创建两个类:团队
和竞争对手
类必须有一个团队
、名称和计算惩罚的方法:列表
public class Team {
private String name;
private List<Competitor> competitors;
// create getters and setters for name and competitors....
// constructor:
public Team(String name) {
this.name = name;
}
public int getScore() {
int totalScore;
// sum all scores of the team using Competitor.getScore()
for (Competitor c : competitors) {
totalScore += c.getScore();
}
return totalScore;
}
}
public class Competitor {
private String name;
private String surname;
// you can also create a Discipline class... but
// I think is not mandatory in this case
private List<Integer> punctuations; // store the punctuations
// create getters and setters for name, surname and punctuations
public int getScore() {
int totalScore = 0;
// sum all scores of the competitor
for (Integer punctuation : punctuations) {
totalScore += punctuation;
}
return totalScore;
}
}
然后,您只需在Main
中处理它,具体取决于您的团队和比赛数量:
public static void main(String[] args) {
// create teams
Team t1 = new Team("T1");
List<Competitors> t1Competitors = new ArrayList<>();
// create competitors
Competitor c1 = new Competitor("John", "Doe");
// fill competitors info
List<Integer> c1Punctuation = new ArrayList<>();
c1Punctuation.add("10");
c1Punctuation.add("20");
c1Punctuation.add("30");
c1.setPunctuation(c1Punctuation);
// put competitors into teams
t1Competitors.add(c1);
t1.setCompetitors(t1Competitors);
// get team punctuations
int t1Score = t1.getScore();
// repeat and compare t1Score and t2Score and so on...
}
publicstaticvoidmain(字符串[]args){
//创建团队
团队t1=新团队(“t1”);
List=newarraylist();
//创造竞争对手
竞争对手c1=新竞争对手(“约翰”、“能源部”);
//填写竞争对手信息
List c1标点符号=new ArrayList();
c1标点符号。添加(“10”);
c1标点符号。添加(“20”);
c1标点符号。添加(“30”);
c1.设置标点符号(c1标点符号);
//将竞争对手分成小组
t1.加入(c1);
t1.赛特竞争者(t1竞争者);
//获取团队标点符号
int t1Score=t1.getScore();
//重复并比较t1Score和t2Score等。。。
}
我已经编写了一个程序,请检查一下。它根据总罚分对球队进行排序
职业选手
class Player{
String name;
Integer d1;
Integer d2;
Integer d3;
Integer d4;
Integer total;
Integer Rank;
//Getters Setters
班队
public class Team {
ArrayList<Player> player;
Integer teamTotal;
public ArrayList<Player> getPlayer() {
return player;
}
public void setPlayer(ArrayList<Player> player) {
this.player = player;
}
public Integer getTeamTotal() {
return teamTotal;
}
public void setTeamTotal(ArrayList<Player> player) {
int tmpTeamTotal=0;
for (Player p : player) {
tmpTeamTotal += p.getTotal();
}
System.out.println(tmpTeamTotal);
teamTotal=tmpTeamTotal;
}
public static Comparator<Team> totalComparator = new Comparator<Team>() {
public int compare(Team tOne, Team tTwo) {
return (tOne.getTeamTotal() - tTwo.getTeamTotal());
}
};
@Override
public String toString()
{
return String.valueOf(this.getTeamTotal());
}
}
公共类团队{
ArrayList播放器;
整数总;
公共阵列列表getPlayer(){
返回球员;
}
公共无效设置播放器(ArrayList播放器){
this.player=player;
}
公共整数getTeamTotal(){
返回总数;
}
公共无效设置总计(ArrayList播放器){
int-tmpTeamTotal=0;
对于(玩家p:玩家){
tmpTeamTotal+=p.getTotal();
}
系统输出打印项次(tmpTeamTotal);
teamTotal=tmpTeamTotal;
}
公共静态比较器totalComparator=新比较器(){
公共整数比较(团队音调、团队tTwo){
返回(tOne.getTeamTotal()-tTwo.getTeamTotal());
}
};
@凌驾
公共字符串toString()
{
返回字符串.valueOf(this.getTeamTotal());
}
}
客户端类
public class Client {
public static void main(String[] args) {
Player pOne = new Player();
pOne.setD1(10);
pOne.setD2(20);
pOne.setD3(30);
pOne.setD4(60);
pOne.setRank(2);
pOne.setName("ABD");
pOne.setTotal(60);
Player pTwo=new Player();
pTwo.setD1(20);
pTwo.setD2(20);
pTwo.setD3(40);
pTwo.setD4(70);
pTwo.setRank(2);
pTwo.setName("SPC");
pTwo.setTotal(60);
ArrayList<Player> playerListOne = new ArrayList<Player>();
playerListOne.add(pOne);
playerListOne.add(pTwo);
Player pTOne = new Player();
pTOne.setD1(10);
pTOne.setD2(70);
pTOne.setD3(30);
pTOne.setD4(90);
pTOne.setRank(2);
pTOne.setName("ABD");
pTOne.setTotal(60);
Player pTTwo=new Player();
pTTwo.setD1(20);
pTTwo.setD2(20);
pTTwo.setD3(40);
pTTwo.setD4(60);
pTTwo.setRank(2);
pTTwo.setName("SPC");
pTTwo.setTotal(80);
ArrayList<Player> playerListTwo = new ArrayList<Player>();
playerListTwo.add(pTOne);
playerListTwo.add(pTTwo);
Team one=new Team();
one.setPlayer(playerListOne);
one.setTeamTotal(playerListOne);
Team two=new Team();
two.setPlayer(playerListTwo);
two.setTeamTotal(playerListTwo);
ArrayList<Team> team=new ArrayList<Team>();
team.add(one);
team.add(two);
Collections.sort(team, Team.totalComparator);
System.out.println(team);
}
}
公共类客户端{
公共静态void main(字符串[]args){
Player pOne=新玩家();
第1组(10);
第2组(20);
第3组(30);
第4组(60);
pOne.setRank(2);
pOne.setName(“ABD”);
pOne.setTotal(60);
Player pTwo=新玩家();
pTwo.setD1(20);
pTwo.setD2(20);
pTwo.setD3(40);
pTwo.setD4(70);
pTwo.setRank(2);
pTwo.setName(“SPC”);
pTwo.setTotal(60);
ArrayList playerListOne=新的ArrayList();
playerListOne.add(pOne);
playerListOne.add(pTwo);
Player pTOne=新玩家();
pTOne.setD1(10);
pTOne.setD2(70);
pTOne.setD3(30);
pTOne.set4(90);
pTOne.setRank(2);
pTOne.setName(“ABD”);
pTOne.setTotal(60);
Player pTTwo=新玩家();
pTTwo.setD1(20);
pTTwo.setD2(20);
pTTwo.setD3(40);
pTTwo.setD4(60);
pTTwo.setRank(2);
pTTwo.设置名称(“SPC”);
pTTwo.setTotal(80);
ArrayList playerListTwo=新的ArrayList();
playerListTwo.add(pTOne);
playerListTwo.add(pTTwo);
团队一=新团队();
一个。赛特球员(playerListOne);
1.塞塔姆道尔(playerListOne);
第二队=新队();
2.setPlayer(playerListTwo);
2.二传总成绩(球员名单2);
ArrayList团队=新建ArrayList();
增加(一个);
增加(两个);
Collections.sort(team,team.totalComparator);
系统输出打印(团队);
}
}
根据数据输入,一线队总罚分:120分
二队总罚分:140分
程序输出:[120140]在类中实现可比接口并覆盖compareTo()method@JordiCastilla:我正在考虑将这些数据组存储到二维数组中,并根据总值对它们进行排序。。但是我不知道这是否是解决这个问题的好方法。@OnderHorka检查我的答案,你必须在这方面做一些工作,但我认为这是满足你需要的正确方法谢谢我会调查的如果你对实施它有疑问,请告诉我:)