当前位置: 首页 > 知识库问答 >
问题:

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

胡高寒
2023-03-14
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);
 }


   }

所以,请,我们被困在试图实现每个队的总积分,并根据总积分获得获胜者

共有1个答案

燕烨
2023-03-14

我建议将points成员变量match移动到team。原因是每个队在任何时间点都会有一些积分,所以每个队都有一个积分场是有意义的。

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

java团队

public class Team {
   private int points;
   // getters and setters for points

   /* Rest of your class */
}
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);
    }
}
 类似资料:
  • 问题内容: 我有下表 在这里,我有一个“学生”表,我想 从该学生表中获取从每个学科获得满分的学生的姓名,例如以下输出。 问题答案: 您可以使用ROW_NUMBER函数仅返回每个主题的“最佳”行: SQL小提琴 MS SQL Server 2008架构设置 : 查询1 : 结果 :

  • 问题内容: 我正在尝试检索当月的哪一天。 例如今天是2011年8月29日。 我想做的只是获取天数,例如29或30。它是每月的哪一天。 我将如何去做? 问题答案: 您需要获取一个Calendar实例并将其作为月份中的某天 您还可以获取DAY_OF_WEEK,DAY_OF_YEAR,DAY_OF_WEEK_IN_MONTH等。

  • 说我有一个地图(myMap)的字符串和对象-地图 如何在myMap中获得K. name每次出现的次数并将其存储在新地图中?新地图将显示K. Name作为键,其值将是它在myMap中出现的次数

  • 本文向大家介绍如何在MongoDB中汇总总和以获得总数?,包括了如何在MongoDB中汇总总和以获得总数?的使用技巧和注意事项,需要的朋友参考一下 要在MongoDB中汇总总和以获取总计数,可以使用$sum运算符。要了解上述概念,让我们使用文档创建一个集合- 在method的帮助下显示集合中的所有文档。查询如下- 以下是输出- 这是获取总数的查询。 情况1-查询如下- 以下是输出- 这是在Mong

  • 问题内容: 我搜索了这个问题,但找不到任何有用的答案。我想获取文档中每个单词的总数,例如,我的索引中有一些推文,并且有一条推文中写着这样的内容:“这里太无聊了,我想去我的家,甜蜜的家”。查询应返回如下响应: 有可能这样做吗? 问题答案: 您正在寻找利用分析仪的。这样做时,您可以定义所需的任何分析器,即阻止分析器将单词转换为根/普通形式。查看文档以获取更多详细信息。 在: 出:

  • 问题内容: 我试图建立具有查询属性,即时通讯修整,以获得与 它不断告诉我,是 无效的 列名我怎么能得到从查询? 问题答案: 用别名工作。Yii 1.1.11。其他失败