从无限的比赛列表输入生成一个足球排名

zpjtge22  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(287)

我有一个作业,我必须编程,从比赛列表输入创建一个排名表。我使用“无限”这个词是因为输入大小未知,所以我必须创建一个程序,直到没有匹配项为止。我为此创建了一个足球类(输入包含两个其他运动和他们自己的比赛和球队,用运动的第一个字母“f,b,v”表示运动类型,例如,他们只是在得分部分不同,所以我认为如果我能使足球工作,我可以使任何其他工作),它包含了积分表中所需的所有内容,匹配结果的方法如下所示:

public class Football {
    private int scoredGoals;
    private int receivedGoals;
    private String teamName;
    private int Score;
    private int wins;
    private int losses;
    private int MatchCount;
    private int draws;

    public void teamValues(String teamName, int Sgoals, int Rgoals) {
        this.teamName = teamName;
        this.scoredGoals = Sgoals;
        this.receivedGoals = Rgoals;

    }           

    public void matched() {
        MatchCount++;
    }

    public void winner() {
        wins++;
    }

    public void draw() {
        draws++;
    }

    public void loser() {
        losses++;
    }

    public void winScore() {
        Score += 3;
    }

    public void drawScore() {
        Score += 1;
    }

    public String showTeams() {
        return (teamName + "    " + MatchCount + "  " + wins + "    " + draws + "   " + losses + "  " + scoredGoals+":"+receivedGoals + "   " + Score);
    }

}

在main类中,我调用if块中的方法来计算获胜、得分、比赛计数等,main如下所示:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

        File file = new File("input.txt");
        Scanner scan = new Scanner(file);
        String fileString = "";
        Football teams[] = new Football[2];
        HashSet<String> teamsArray = new HashSet<String>();

        while(scan.hasNextLine()) {
            fileString = scan.nextLine();
            String[] match = fileString.split("\\t|:");

            if(match[0].equals("F")) {
                int team1score = Integer.valueOf(match[3].trim());
                int team2score = Integer.valueOf(match[4].trim());

                teams[0] = new Football();
                teams[0].teamValues(match[1], team1score, team2score);
                teams[1] = new Football();
                teams[1].teamValues(match[2], team2score, team1score);
                teams[0].matched();
                teams[1].matched();
                if(team1score>team2score) {

                    teams[0].winner();
                    teams[1].loser();

                    teams[0].winScore();
                }

                if(team1score==team2score) {
                    teams[0].draw();
                    teams[1].draw();

                    teams[0].drawScore();
                    teams[1].drawScore();
                }

                if(team1score<team2score) {
                    teams[1].winner();
                    teams[0].loser();

                    teams[1].winScore();
                }

            String team0 = teams[0].showTeams();
            String team1 = teams[1].showTeams();

            teamsArray.add(team0);
            teamsArray.add(team1);

        }   
    }
        scan.close();
    }   
}

因为输入是静态的,所以我使用数组来处理。我的代码的问题是,我找不到一种方法来存储我的团队,而没有重复项和变量,只要团队有另一个匹配项,这些变量就会出现并更新。
我试过了;
将它们存储在2d字符串数组中,但由于团队数量未知,我认为这不是解决问题的一种健康方法。
将它们存储在string[]数组列表中,最后存储的是地址而不是团队的值。
我仍然使用它来检查至少这些方法是否按预期工作。
我觉得这个计划触礁了,我需要重新开始,所以任何建议都很感激。
下面是一个输入和输出示例:

Input:
    Home Team       Guest Team         H : G
F   Manchester U.   Chelsea            2 : 2
F   Liverpool       Manchester City    3 : 2
F   Leicester City  Everton            1 : 3
V   Team A          Team B             3 : 0
F   Tottenham       Liverpool          3 : 1
B   Team B          Team A            90 : 96
F   West Ham        Manchester U.      2 : 1
F   Arsenal         Manchester City    0 : 2
F   Chelsea         Arsenal            3 : 3

Output: 
    Name          Matches Wins Draw Lose   Scored:Received Score
1.  Manchester U.   10     6    2    2         27:22        20
2.  Arsenal         10     6    2    2         25:24        20 
3.  Chelsea         10     5    3    2         28:20        18
4.  Liverpool       10     4    4    2         22:19        16
5.  Tottenham       10     4    4    2         22:21        16

有些球队的得分是相同的,因为计算得分和收到的进球的平均数是对球队进行排序的另一种方法。

umuewwlo

umuewwlo1#

首先是对 Football 班级:
覆盖 equals 能够搜索列表
覆盖 compareTo 用于分类
覆盖 toString 而不是 showTeams 创建构造函数
将大多数功能合并到 teamValues ```
import java.util.Formatter;

public class Football implements Comparable {
private int scoredGoals;
private int receivedGoals;
private String teamName;
private int score;
private int wins;
private int losses;
private int draws;
private int matchCount;

public int compareTo(Football f) {
    return score - f.score;
}

public boolean equals(Object o) {
    if (o == null) {
        return false;
    }
    else if (o instanceof Football) {
        return teamName.equals(((Football)o).teamName);
    }
    else if (o instanceof String) {
        return teamName.equals((String)o);
    }
    return false;
}

public Football(String teamName) {
    this.teamName = teamName;
}

public void teamValues(int scoredGoals, int receivedGoals) {
    this.scoredGoals += scoredGoals;
    this.receivedGoals += receivedGoals;
    matchCount++;
    if (scoredGoals < receivedGoals) {
        losses++;
    }
    else if (scoredGoals > receivedGoals) {
        wins++;
        score += 3;
    }
    else {
        draws++;
        score += 1;
    }
}           

public String toString() {
    return new Formatter().format("%-20s %3d %3d %3d %3d %3d:%-3d %d",
        teamName, matchCount, wins, draws, losses, scoredGoals, receivedGoals, score)
        .toString();
} 

}

对于主程序,您不希望每次都创建一个新的团队—仅当在文件中首次遇到团队时。把所有的队伍放在一个 `List` . 解析新游戏时,首先尝试在列表中查找团队。如果不在里面,就加进去。

List teamsArray = new ArrayList<>();

while (scan.hasNextLine()) {
fileString = scan.nextLine();
String[]match = fileString.split("\t|:");

if (match.length == 5 && match[0].equals("F")) {
    int team1score = Integer.valueOf(match[3].trim());
    int team2score = Integer.valueOf(match[4].trim());

    // Create a temp team to search the List
    Football team1 = new Football(match[1]);
    // Search the list
    if (!teamsArray.contains(team1)) {
        // Not in the list already. Add it
        teamsArray.add(team1);
    }
    else {
        // Already in the List. Use that one.
        team1 = teamsArray.get(teamsArray.indexOf(team1));
    }
    // Repeat for team 2
    Football team2 = new Football(match[2]);
    if (!teamsArray.contains(team2)) {
        teamsArray.add(team2);
    }
    else {
        team2 = teamsArray.get(teamsArray.indexOf(team2));
    }
    team1.teamValues(team1score, team2score);
    team2.teamValues(team2score, team1score);
}

}
System.out.println("Name M W D L S:R S");
// Sort and print
teamsArray
.stream()
.sorted(Comparator.reverseOrder())
.forEach(t -> System.out.println(t));

相关问题