如何在锦标赛中获得每支球队的总分并获得胜利者

我们想设计一个简单的锦标赛,由具有名称和公民身份的团队组成。在本次比赛中,受邀球队之间进行了一组比赛,每场比赛对阵两支球队。得分最高的球队赢得比赛。如果比赛结果是平局,每队得 1 分,胜者得 2 分,负者不得分。我们想获得一个团队在比赛中的总积分来知道获胜者。获胜者是得分最高的人。


因此我们设法创建了三个类:Team、Match 和 Tournament 以及主类。


在主类中,我们有这个


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 + '}';

  }


慕田峪4524236
浏览 116回答 1
1回答

HUX布斯

我建议将points成员变量从移动Match到Team。原因是每个团队在任何时间点都会有一些积分,因此每个团队都有一个积分字段是有道理的。现在您将对方法进行以下更改团队.javapublic class Team {   private int points;   // getters and setters for points   /* Rest of your class */}匹配.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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java