我们想设计一个简单的比赛,由具有姓名和国籍的团队组成。在本次比赛中,受邀球队之间进行了一场比赛,每场比赛都与两队相对。得分最高的球队赢得比赛。如果比赛结果为平局,则每支球队得1分,获胜的球队得2分,输家则得不到分。我们希望获得比赛中一支球队的总积分,以了解获胜者。获胜者是得分最高的人。
因此我们设法创建了三个班级:团队,比赛和锦标赛以及主要班级。
在主班我们有这个
public class ProgramTournaments {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Defining each team
Team frTeam, inTeam, cnTeam;
//Creation of three objects (Teams)
frTeam = new Team("French Blue Team", "French"); // New Means I want to create an Object (frTeams)
inTeam = new Team("Indian Blue Team", "India");
cnTeam = new Team("Chinese Red Team", "China");
//Create a new Tournament
Tournament tournament = new Tournament();
//Invite teams to the tourname
tournament.inviteTeam(frTeam);
tournament.inviteTeam(inTeam);
tournament.inviteTeam(cnTeam);
//Add matches to Tournament
Match m1 = new Match(frTeam, inTeam, true);
Match m2 = new Match(frTeam, cnTeam, true);
Match m3 = new Match(inTeam, cnTeam, true);
tournament.addMatch(m1);
tournament.addMatch(m2);
tournament.addMatch(m3);
//Check If all matches Have been Pleayed
tournament.allMatchPlayed();
}
}
我们在团队课上做到了
public class Team {
//Defining the attributes
private String name; //Private means it is limited only to this Class (team)
private String citizenship;
public String getName() {
return name;
}
public String getCitizenship() {
return citizenship;
}
// Constructor inorder to initialized values
public Team (String name, String citizenship){
this.name = name; //Initializing name of team
this.citizenship = citizenship; //Initializing name of Citizenship of team
}
//Printing to strings
@Override
public String toString() {
return "Team{" + "name=" + name + ", citizenship=" + citizenship + '}';
}
}
在Match类中,我们做到了
public class Match {
private Team team1, team2;
private int scoreTeam1;
private int scoreTeam2;
private int pointTeam1, pointTeam2;
boolean play;
//Constructor
public Match(Team team1, Team team2, boolean play) {
this.team1 = team1;
this.team2 = team2;
this.scoreTeam1 = generateRandomScore();
this.scoreTeam2 = generateRandomScore();
this.play = play;
}
//All Methods
public int getScoreTeam1() {
return scoreTeam1;
}
public void setScoreTeam1(int scoreTeam1) {
this.scoreTeam1 = scoreTeam1;
}
public int getScoreTeam2() {
return scoreTeam2;
}
public void setScoreTeam2(int scoreTeam2) {
this.scoreTeam2 = scoreTeam2;
}
public Team getTeam1() {
return team1;
}
public void setTeam1(Team team1) {
this.team1 = team1;
}
public Team getTeam2() {
return team2;
}
public void setTeam2(Team team2) {
this.team2 = team2;
}
public boolean isPlay() {
return play;
}
public void setPlay(boolean play) {
this.play = play;
}
//Generate Random Score
private int generateRandomScore() {
Random random = new Random();
return random.nextInt(5);
}
public boolean draw() {
if (scoreTeam1 == scoreTeam2) {
pointTeam1 = 1;
pointTeam2 = 1;
return true;
}
return false;
}
public Team matchWinner() {
if (scoreTeam1 > scoreTeam2) {
pointTeam1 = 2;
pointTeam2 = 0;
return team1;
} else {
pointTeam2 = 2;
pointTeam1 = 0;
return team2;
}
}
}
在锦标赛课程中,我们做到了
public class Tournament {
private List<Team> ListOfTeams = new ArrayList<>();
private List<Match> ListOfMatches = new ArrayList<>();
//Methods
public void inviteTeam(Team team) { //Inviting Teams
ListOfTeams.add(team);
}
public void addMatch(Match m) {
ListOfMatches.add(m);
}
public boolean allMatchPlayed() {
for (Match match : ListOfMatches) {
if (match.isPlay() == false) {
return false;
}
}
return true;
}
public void tournamentWinner(){
for (Match match : ListOfMatches){
match.decideResult();
}
Comparator <Team> team = new Comparator<Team>(){
@override
public int compare(Team t1, Team t2){
return t1.getScore() - t2.getScore();
}
};
Collections.sort(ListOfTeams, t);
System.out.println("The winner of the tournament is: " + ListOfTeams);
}
}
所以,请您坚持执行每个团队的总积分,并根据总积分获得获胜者
答案 0 :(得分:1)
我建议将points
成员变量从Match
移到Team
。原因是每个团队在任何时间点都会有一些积分,因此每个团队都有一个积分字段是有道理的。
现在您将对方法进行以下更改
Team.java
public class Team {
private int points;
// getters and setters for points
/* Rest of your class */
}
Match.java
我们应该将您的draw()
和matchWinner()
合并为一种方法decideResult()
,因为它们自己没有意义。
public void decideResult() {
if (scoreTeam1 == scoreTeam2) {
team1.setPoints(team1.getPoints() + 1);
team2.setPoints(team2.getPoints() + 1);
} else if (scoreTeam1 > scoreTeam2) {
team1.setPoints(team1.getPoints() + 2);
} else {
team2.setPoints(team2.getPoints() + 2);
}
}
要找到获胜者,您只需从相应的Team
对象中获取分数即可。例如:frTeam.getPoints()
并将其与其他国家/地区.getPoints()