我正在编写一个用于评估比赛积分和排名的应用程序,我有这个具体问题:
我有一些团队,每个团队由4个竞争者组成。团队中的每个成员都有一些罚分,然后为整个团队总结。例如:
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
依旧......
我需要根据总小区/总小区的总罚分来对这些小组进行排序。任何提示如何有效地解决这个问题?这是我在java中的第一个应用程序,所以我将不胜感激任何帮助。
答案 0 :(得分:0)
您应该使用List来存储Team类实例,以便您可以轻松地对它们进行排序。查看Comparator和Comparable接口的示例。团队类将包含teamMembers,它存储带有惩罚的TeamMember类实例,TeamMember应该有惩罚总和的getter方法。这个方法将从团队实例调用到账户团队罚款总和。因此,在您的比较中,您将能够调用团队方法,返回团队成员罚款的总和。
答案 1 :(得分:0)
只要我不知道团队或竞争对手是否会拥有超过示例数据,并且这是您的第一个Java
应用程序之一,我建议您使用简单的对象来解决此问题OOP
创建两个类:Team
和Competitor
。
Team
类必须有List<Competitor>
,名称和计算惩罚的方法:<强> Team.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;
}
}
Competitor
类必须有List<Integer>
,名称,姓氏和计算实体得分的方法:<强> Competitor.java 强>
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...
}
答案 2 :(得分:0)
我写了一个程序检查出来。它根据总罚分来对球队进行分类。
班级球员
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());
}
}
客户端类
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);
}
}
根据数据输入第一队总罚分:120 二队总罚球点数:140 节目输出:[120,140]