私はJavaの初心者です。これが私の最初の投稿です。 この記事は、類似したように見えたが、私はまさに私の問題のように何かを見つけることができませんでした: Why is this print line command executing twice?forループの最後の行は2回実行されますか?
が、答えは私がそれを解決する助けにはなりませんでした。
私はそれがおそらく何かばかげたことを知っていますが、皆さんのうちの一人が「マッチ」という名前の配列の最後のエントリが2度印刷される理由を指摘できるかもしれないことを期待していました。
ありがとうございます。 Robert。それが2回印刷した場合、最も可能性の高い説明は、最後の2つのエントリが同じであることである、
public String buildMatchList(Match[] matches)
{
fixtures = "";
int i = 0;
for (i = 0; i < numMatches; i++)
{
if (matches[i] != null)
{
fixtures += String.format("\n%-10.10s %10.9s %15.14s", matches[i].getTeamA(), " Vs ", matches[i].getTeamB());
}
}
System.out.println(fixtures);
}
// -EDIT -
// numMatches set in this method
public void fillMatchArray(Team[] sortedTeams, int numTeams)
{
int homeTeam = 0;
int awayTeam = 0;
goalsA = 0;
goalsB = 0;
fixtures = "";
boolean played = false;
matches = new Match[MAX_NUM_GAMES];
for (homeTeam = 0; homeTeam < sortedTeams.length; homeTeam++)
for (awayTeam = homeTeam+1; awayTeam < sortedTeams.length; awayTeam++)
{
String teamA = sortedTeams[homeTeam].getTeamName();
String teamB = sortedTeams[awayTeam].getTeamName();
matchFixtures = new Match(teamA, teamB, goalsA, goalsB, played);
{
fixtures += String.format("\n%-10.10s %10.9s %15.14s",
matchFixtures.getTeamA(), " Vs ", matchFixtures.getTeamB());
}
int i = 0;
matches[i] = matchFixtures;
numMatches++;
buildMatchList(matches);
}
}
変数「numMatches」はどこに設定されていますか? –
最後に2つのエントリが同じであることを確認してください。また、 'matches'を埋めている場所にスニペットを入れますか? – havexz