用java写一个比赛胜败组
时间: 2023-11-27 17:52:18 浏览: 37
以下是一个简单的 Java 代码示例,用于比较两支队伍的胜负记录,以确定哪个团队胜利:
```java
public class Team {
private String name;
private int wins;
private int losses;
public Team(String name, int wins, int losses) {
this.name = name;
this.wins = wins;
this.losses = losses;
}
public String getName() {
return name;
}
public int getWins() {
return wins;
}
public int getLosses() {
return losses;
}
}
public class Game {
private Team homeTeam;
private Team awayTeam;
private int homeScore;
private int awayScore;
public Game(Team homeTeam, Team awayTeam, int homeScore, int awayScore) {
this.homeTeam = homeTeam;
this.awayTeam = awayTeam;
this.homeScore = homeScore;
this.awayScore = awayScore;
}
public Team getHomeTeam() {
return homeTeam;
}
public Team getAwayTeam() {
return awayTeam;
}
public int getHomeScore() {
return homeScore;
}
public int getAwayScore() {
return awayScore;
}
}
public class Matchup {
private Game[] games;
public Matchup(Game[] games) {
this.games = games;
}
public Team getWinner() {
int homeWins = 0;
int awayWins = 0;
for (Game game : games) {
if (game.getHomeScore() > game.getAwayScore()) {
homeWins++;
} else {
awayWins++;
}
}
if (homeWins > awayWins) {
return games[0].getHomeTeam();
} else {
return games[0].getAwayTeam();
}
}
}
```
使用示例:
```java
Team team1 = new Team("Team 1", 10, 5);
Team team2 = new Team("Team 2", 8, 7);
Team team3 = new Team("Team 3", 12, 3);
Game[] games = new Game[]{
new Game(team1, team2, 90, 85),
new Game(team2, team3, 70, 80),
new Game(team1, team3, 100, 95)
};
Matchup matchup = new Matchup(games);
Team winner = matchup.getWinner();
System.out.println("The winner is " + winner.getName());
```
输出:
```
The winner is Team 1
```
在这个示例中,我们首先创建了三个球队,每个球队都有自己的胜利和失败记录。然后,我们创建了三场比赛,并设置了每个团队的得分。最后,我们将这些游戏传递给 Matchup 类,该类比较每个团队在这些游戏中的胜利和失败记录,并确定胜者。在这种情况下,Team 1 获得了两场游戏的胜利,因此被宣布为胜利者。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)