Java 如何在一场锦标赛中获得每支球队的总分并获得冠军

Java 如何在一场锦标赛中获得每支球队的总分并获得冠军,java,class,oop,Java,Class,Oop,我们想设计一个简单的锦标赛,由有名字和国籍的球队组成。在本次锦标赛中,受邀球队之间组织了一系列比赛,每场比赛都与两队相对。得分最高的队获胜。如果比赛结果是平局,每队得1分,获胜队得2分,失败者得不到分。我们想知道一支球队在一场比赛中的总得分,从而知道胜利者。赢家是得分最高的人 因此,我们设法创建了三个类:团队、比赛和锦标赛以及主类 在主课上我们有这个 public class ProgramTournaments { /** * @param args the command line ar

我们想设计一个简单的锦标赛,由有名字和国籍的球队组成。在本次锦标赛中,受邀球队之间组织了一系列比赛,每场比赛都与两队相对。得分最高的队获胜。如果比赛结果是平局,每队得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 + '}';
} 
  }
在比赛课上我们这样做了

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);
 }


   }
在比赛课上我们这样做了

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);
 }


   }
公开课比赛{
private List ListOfTeams=new ArrayList();
private List ListOfMatches=new ArrayList();
//方法
公共团队(团队团队){//邀请团队
团队列表。添加(团队);
}
公共无效添加匹配(匹配m){
匹配列表。添加(m);
}
公共布尔值allMatchPlayed(){
for(匹配:匹配列表){
if(match.isPlay()==false){
返回false;
}
}
返回true;
}
公开无效锦标赛冠军(){
for(匹配:匹配列表){
match.decideResult();
}
比较器团队=新比较器(){
@凌驾
公共整数比较(t1组、t2组){
返回t1.getScore()-t2.getScore();
}
};
Collections.sort(团队列表,t);
System.out.println(“锦标赛的获胜者是:“+ListOfTeams”);
}
}

因此,请大家注意,我们一直在努力实现每个团队的总分,并根据总分获得优胜者

我建议将
积分
成员变量从
匹配
移动到
团队
。原因是每个队在任何时候都会有一些分数,所以每个队都有一个积分场是有道理的

现在您将对这些方法进行以下更改

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);
    }
}

要找到获胜者,您只需从相应的
团队
对象中获取分数即可。例如:
frTeam.getPoints()
并将其与其他国家/地区进行比较
.getPoints()

为什么不将points成员变量从
Match
移动到
Team
?这更有意义,因为每支球队都会得到一些分数。我们所做的是,在比赛课上,一支球队在一场比赛中可以得到0分、1分或2分。我们现在要做的是,在我们创建的比赛中,为每个队增加分数,为每个比赛增加分数,但我们不知道在哪里存储递增的变量,该变量计算每个队的总分数。非常感谢,这非常有帮助。我们添加了matchWinner,看起来还可以。现在在主课堂上,我们刚刚创建了三个团队来尝试我们的程序,但是以后我们会有很多团队,所以很难逐一比较每个团队的分数。你有什么想法吗?如果你有许多团队,你可以将所有团队添加到一个集合中,并按点数排序。这将消除您的手动操作。关于DeciderResult(),我们更希望保留draw()和matchWinner(),因为稍后我们希望在draw match的情况下添加超时,如果它是直接来自
DeciderResult()的draw,为什么不调用超时方法呢
:)我们尝试按照您的建议进行比较,以获得获胜者,但我们在这方面遇到了问题。我们试图在比赛课上进行比较,但我们被搞糊涂了?你能帮我们吗?谢谢