英文:
Java - Avoid/Solve StackOverFlow
问题
这个问题出现在你的循环中。循环中的pistes
数组的长度是5,但matches
的大小却可能小于5,因此你需要确保pistes
数组不越界。你可以使用取模运算来处理这个问题,如下所示:
for (Round round : rounds) {
System.out.println("Round " + round.getNumber());
List<Match> matches = round.getMatches();
for (int i = 0; i < pistes.length; i++) {
if (i < matches.size()) {
Match match = matches.get(i);
String piste = pistes[i];
System.out.println(piste + " " + match.getTeam1().getName() + " vs " + match.getTeam2().getName());
}
}
System.out.println();
}
这将确保只在matches
中有匹配时才使用pistes
中的元素,防止数组越界错误。
英文:
this is the code
import java.util.ArrayList;
import java.util.List;
public class ScheduleGenerator {
public static void main(String[] args) {
List<Team> teams = new ArrayList<>();
int numTeams = 9;
for (int i = 1; i <= numTeams; i++) {
teams.add(new Team("Team " + i));
}
String[] pistes = {"Piste A", "Piste B", "Piste C", "Piste D", "Piste E", "Piste F"};
List<Round> rounds = generateSchedule(teams);
for (Round round : rounds) {
System.out.println("Round " + round.getNumber());
List<Match> matches = round.getMatches();
for (int i = 0; i < matches.size(); i++) {
Match match = matches.get(i);
String piste = pistes[i];
System.out.println(piste + " " + match.getTeam1().getName() + " vs " + match.getTeam2().getName());
}
System.out.println();
}
}
public static List<Round> generateSchedule(List<Team> teams) {
int numTeams = Math.round(teams.size());
int numRounds = numTeams;
int halfSize = numTeams / 2;
List<Team> list = new ArrayList<>(teams);
int listSize = list.size();
List<Round> rounds = new ArrayList<>();
if (numTeams % 2 != 0) {
teams.add(new Team("Rest"));
numTeams++;
}
for (int round = 0; round < numRounds; round++) {
Round currentRound = new Round(round);
List<Match> matches = new ArrayList<>();
int teamIdx = round % listSize;
Team team1 = list.get(teamIdx);
Team team2 = teams.get(numTeams - 1);
matches.add(new Match(team1, team2));
for (int i = 1; i < halfSize; i++) {
int firstTeam = (round + i) % listSize;
int secondTeam = (round + listSize - i) % listSize;
matches.add(new Match(list.get(firstTeam), list.get(secondTeam)));
}
currentRound.setMatches(matches);
rounds.add(currentRound);
}
return rounds;
}
}
class Team {
private String name;
public Team(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString() {
return name;
}
}
class Round {
private int number;
private List<Match> matches;
public Round(int number) {
this.number = number;
this.matches = new ArrayList<>();
}
public int getNumber() {
return number;
}
public void addMatch(Match match) {
matches.add(match);
}
public void setMatches(List<Match> matches) {
this.matches = matches;
}
public List<Match> getMatches() {
return matches;
}
public boolean hasMatch(Team team1, Team team2) {
for (Match match : matches) {
if ((match.getTeam1() == team1 && match.getTeam2() == team2) ||
(match.getTeam1() == team2 && match.getTeam2() == team1)) {
return true;
}
}
return false;
}
}
class Match {
private Team team1;
private Team team2;
public Match(Team team1, Team team2) {
this.team1 = team1;
this.team2 = team2;
}
public Team getTeam1() {
return team1;
}
public Team getTeam2() {
return team2;
}
}
The output needs to be like this
Round 0
Piste A Team 1 vs Team 9
Piste B Team 2 vs Team 8
Piste C Team 3 vs Team 7
Piste D Team 4 vs Team 6
Piste E Rest vs Team 5
Round 1
Piste A Team 1 vs Team 9
Piste B Team 2 vs Team 8
Piste C Team 3 vs Team 7
Piste D Team 4 vs Team 6
Piste E Rest vs Team 5
Round 2
Piste A Team 9 vs Team 8
Piste B Team 1 vs Team 7
Piste C Team 2 vs Team 6
Piste D Team 3 vs Team 5
Piste E Rest vs Team 4
Round 3
Piste A Team 8 vs Team 7
Piste B Team 9 vs Team 6
Piste C Team 1 vs Team 5
Piste D Team 2 vs Team 4
Piste E Rest vs Team 3
Round 4
Piste A Team 7 vs Team 6
Piste B Team 8 vs Team 5
Piste C Team 9 vs Team 4
Piste D Team 1 vs Team 3
Piste E Rest vs Team 2
Round 5
Piste A Team 6 vs Team 5
Piste B Team 7 vs Team 4
Piste C Team 8 vs Team 3
Piste D Team 9 vs Team 2
Piste E Rest vs Team 1
Round 6
Piste A Team 5 vs Team 4
Piste B Team 6 vs Team 3
Piste C Team 7 vs Team 2
Piste D Team 8 vs Team 1
Piste E Rest vs Team 9
Round 7
Piste A Team 4 vs Team 3
Piste B Team 5 vs Team 2
Piste C Team 6 vs Team 1
Piste D Team 7 vs Team 9
Piste E Rest vs Team 8
Round 8
Piste A Team 3 vs Team 2
Piste B Team 4 vs Team 1
Piste C Team 5 vs Team 9
Piste D Team 6 vs Team 8
Piste E Rest vs Team 7
Round 9
Piste A Team 2 vs Team 1
Piste B Team 3 vs Team 9
Piste C Team 4 vs Team 8
Piste D Team 5 vs Team 7
Piste E Rest vs Team 6
but as you can see in the first bit of the code here
for (Round round : rounds) {
System.out.println("Round " + round.getNumber());
List<Match> matches = round.getMatches();
for (int i = 0; i < matches.size(); i++) {
Match match = matches.get(i);
String piste = pistes[i];
System.out.println(piste + " " + match.getTeam1().getName() + " vs " + match.getTeam2().getName());
}
System.out.println();
}
this loop makes it that the maximum index is 4 that means that every time I run this code it will be missing Piste E and the output will be something like this
Round 0
Piste A Team 1 vs Rest
Piste B Team 2 vs Team 9
Piste C Team 3 vs Team 8
Piste D Team 4 vs Team 7
Round 1
Piste A Team 2 vs Rest
Piste B Team 3 vs Team 1
Piste C Team 4 vs Team 9
Piste D Team 5 vs Team 8
Round 2
Piste A Team 3 vs Rest
Piste B Team 4 vs Team 2
Piste C Team 5 vs Team 1
Piste D Team 6 vs Team 9
Round 3
Piste A Team 4 vs Rest
Piste B Team 5 vs Team 3
Piste C Team 6 vs Team 2
Piste D Team 7 vs Team 1
Round 4
Piste A Team 5 vs Rest
Piste B Team 6 vs Team 4
Piste C Team 7 vs Team 3
Piste D Team 8 vs Team 2
Round 5
Piste A Team 6 vs Rest
Piste B Team 7 vs Team 5
Piste C Team 8 vs Team 4
Piste D Team 9 vs Team 3
Round 6
Piste A Team 7 vs Rest
Piste B Team 8 vs Team 6
Piste C Team 9 vs Team 5
Piste D Team 1 vs Team 4
Round 7
Piste A Team 8 vs Rest
Piste B Team 9 vs Team 7
Piste C Team 1 vs Team 6
Piste D Team 2 vs Team 5
Round 8
Piste A Team 9 vs Rest
Piste B Team 1 vs Team 8
Piste C Team 2 vs Team 7
Piste D Team 3 vs Team 6
Round 9
Piste A Team 1 vs Rest
Piste B Team 2 vs Team 9
Piste C Team 3 vs Team 8
Piste D Team 4 vs Team 7
I am just really ignorant with this type of error it's been almost 3 days with me trying to figure out a thing. Thanks in advance
答案1
得分: 0
int halfSize = numTeams / 2;
应该改成 int halfSize = numTeams / 2 + 1;
。
英文:
Just Figure it out int halfSize = numTeams / 2;
should be added by one like this int halfSize = numTeams / 2 +1;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论