我正在尝试创建一个足球联赛模拟器[关闭]

问题描述 投票:0回答:0

这是我的 Java 代码

package com.alviss.football.sim;

import java.util.*;

public class LeagueSimulator {
    private List<String> teams;
    private int numMatches;
    private Map<String, Integer> matchesPlayedTable;
    private Map<String, Integer> matchesWonTable;
    private Map<String, Integer> matchesLostTable;
    private Map<String, Integer> matchesDrawnTable;
    private Map<String, Integer> goalsScoredTable;
    private Map<String, Integer> goalsConcededTable;
    private Map<String, Integer> goalDiffTable;
    private Map<String, Integer> pointsTable;



    public LeagueSimulator(List<String> teams, int numMatches) {
        this.teams = teams;
        this.numMatches = numMatches;
        this.matchesPlayedTable = new HashMap<String, Integer>();
        this.matchesWonTable = new HashMap<String, Integer>();
        this.matchesLostTable = new HashMap<String, Integer>();
        this.matchesDrawnTable = new HashMap<String, Integer>();
        this.goalsScoredTable = new HashMap<String, Integer>();
        this.goalsConcededTable = new HashMap<String, Integer>();
        this.goalDiffTable = new HashMap<String, Integer>();
        this.pointsTable = new HashMap<String, Integer>();



        // Initialize points, goals scored, and goals conceded for each team
        for (String team : teams) {
            matchesPlayedTable.put(team, 0);
            matchesWonTable.put(team, 0);
            matchesLostTable.put(team, 0);
            matchesDrawnTable.put(team, 0);
            goalsScoredTable.put(team, 0);
            goalsConcededTable.put(team, 0);
            goalDiffTable.put(team, 0);
            pointsTable.put(team, 0);
        }
    }

    public void simulateMatches() {
        int numRounds = numMatches / 2;
        List<String> playedFixtures = new ArrayList<String>();
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);

        // Simulate matches for each round
        for (int round = 1; round <= numRounds; round++) {
            System.out.println("Matchday " + round + " Fixtures:");

            // Shuffle the teams to randomize the fixtures
            Collections.shuffle(teams);

            // Play the fixtures for this round
            for (int i = 0; i < teams.size(); i += 2) {
                String homeTeam = teams.get(i);
                String awayTeam = teams.get(i + 1);

                // Check if this fixture has already been played
                String fixture = homeTeam + " vs " + awayTeam;
                if (playedFixtures.contains(fixture)) {
                    continue;
                }

                // Print the fixture and get the user input for the score
                System.out.print(homeTeam + " vs " + awayTeam + ": ");
                int homeGoals = scanner.nextInt();int awayGoals = scanner.nextInt();


                // Update the matches played, won, lost, drawn , goals scored & conceded, goal difference & points for each team
                matchesPlayedTable.put(homeTeam, matchesPlayedTable.get(homeTeam) + homeGoals);
                matchesPlayedTable.put(awayTeam, matchesPlayedTable.get(awayTeam) + awayGoals);
                matchesWonTable.put(homeTeam, matchesWonTable.get(homeTeam) + homeGoals);
                matchesWonTable.put(awayTeam, matchesWonTable.get(awayTeam) + awayGoals);
                matchesLostTable.put(homeTeam, matchesLostTable.get(homeTeam) + homeGoals);
                matchesLostTable.put(awayTeam, matchesLostTable.get(awayTeam) + awayGoals);
                matchesDrawnTable.put(homeTeam, matchesDrawnTable.get(homeTeam) + homeGoals);
                matchesDrawnTable.put(awayTeam, matchesDrawnTable.get(awayTeam) + awayGoals);
                goalsScoredTable.put(homeTeam, goalsScoredTable.get(homeTeam) + homeGoals);
                goalsScoredTable.put(awayTeam, goalsScoredTable.get(awayTeam) + awayGoals);
                goalsConcededTable.put(homeTeam, goalsConcededTable.get(homeTeam) + awayGoals);
                goalsConcededTable.put(awayTeam, goalsConcededTable.get(awayTeam) + homeGoals);
                goalDiffTable.put(homeTeam, goalDiffTable.get(homeTeam) + homeGoals);
                goalDiffTable.put(awayTeam, goalDiffTable.get(awayTeam) + awayGoals);
                pointsTable.put(homeTeam, pointsTable.get(homeTeam) + (homeGoals > awayGoals ? 3 : (homeGoals == awayGoals ? 1 : 0)));
                pointsTable.put(awayTeam, pointsTable.get(awayTeam) + (awayGoals > homeGoals ? 3 : (awayGoals == homeGoals ? 1 : 0)));

                // Add the fixture to the list of played fixtures
                playedFixtures.add(fixture);
            }
        }

        // Close the scanner
        scanner.close();
    }
    public void printTable() {
        // Sort the teams by matches played, won, lost drawn, goals scored, goals conceded, goal difference & points
        List<String> sortedTeams = new ArrayList<String>(teams);
        Collections.sort(sortedTeams, (a, b) -> {
            int matchesPlayedCompare = Integer.compare(matchesPlayedTable.get(b), matchesPlayedTable.get(a));
            if (matchesPlayedCompare != 0) {
                return matchesPlayedCompare;
            }
            int matchesWonCompare = Integer.compare(matchesWonTable.get(b), matchesWonTable.get(a));
            if (matchesWonCompare != 0) {
                return matchesWonCompare;
            }
            int matchesLostCompare = Integer.compare(matchesLostTable.get(b), matchesLostTable.get(a));
            if (matchesLostCompare != 0) {
                return matchesLostCompare;
            }
            int matchesDrawnCompare = Integer.compare(matchesDrawnTable.get(b), matchesDrawnTable.get(a));
            if (matchesDrawnCompare != 0) {
                return matchesDrawnCompare;
            }
            int goalsScoredCompare = Integer.compare(goalsScoredTable.get(b), goalsScoredTable.get(a));
            if (goalsScoredCompare != 0) {
                return goalsScoredCompare;
            }
            int goalsConcededCompare = Integer.compare(goalsConcededTable.get(b), goalsConcededTable.get(a));
            if (goalsConcededCompare != 0) {
                return goalsConcededCompare;
            }
            int goalDifferenceCompare = Integer.compare((goalsScoredTable.get(b) - goalsConcededTable.get(b)), (goalsScoredTable.get(a) - goalsConcededTable.get(a)));
            if (goalDifferenceCompare != 0) {
                return goalDifferenceCompare;
            }
            int pointsCompare = Integer.compare(pointsTable.get(b), pointsTable.get(a));
            if (pointsCompare != 0) {
                return pointsCompare;
            }
            return a.compareTo(b);
        });

        // Print the table header
        System.out.println("Team\tP\tW\tL\tD\tGF\tGA\tGD\tPTS");

        // Print each team's stats
        for (String team : sortedTeams) {
            System.out.println(team + "\t" + matchesPlayedTable.get(team) + "\t" + matchesWonTable.get(team) + "\t" +
                    matchesLostTable.get(team) + "\t" + matchesDrawnTable.get(team) + goalsScoredTable.get(team) + + goalsConcededTable.get(team) + "\t\t" +
                    goalDiffTable.get(team) + "\t" + pointsTable.get(team) + "\t");
        }
    }
    public static void main(String[] args) {
        List<String> teams = new ArrayList<String>();
        teams.add("Team A");
        teams.add("Team B");
        teams.add("Team C");
        teams.add("Team D");
        teams.add("Team E");
        teams.add("Team F");
        teams.add("Team G");
        teams.add("Team H");
        teams.add("Team I");
        teams.add("Team J");

        int numMatches = teams.size() * 2;

        LeagueSimulator simulator = new LeagueSimulator(teams, numMatches);
        simulator.simulateMatches();
        simulator.printTable();
    }
}

一切似乎都运行良好,代码运行除了计算错误。

程序错误地打印了联赛的最终排名。

应该有18场比赛,因此每支球队应该打18场比赛。

总积分计算如下:胜=3分,平=1分,负=0分。

净胜球的计算方式为:进球数-失球数。

进球数是球队在整个赛季的进球数。

失球数是一支球队整个赛季失球数。

赢得的比赛是一支球队在整个赛季中赢得的比赛数量。

输掉的比赛是球队整个赛季输掉的比赛数量。

平局数是一支球队整个赛季平局的比赛数。

我还希望在每个比赛日回合结束时根据之前的比赛日结果打印并显示最终表格,然后用户才能开始输入下一个比赛日回合的结果

当我运行程序时,它错误地打印了决赛桌,我不知道如何根据之前的比赛日结果在每轮比赛后打印决赛桌。

java simulator sports-league-scheduling-problem
© www.soinside.com 2019 - 2024. All rights reserved.