我正在Netbeans写一个梦幻篮球队计划,其中10支球队对阵每支球队打2场比赛,我试图跟踪每支球队的输赢数量。十个团队被存储到一个数组列表中,我有两个循环设置迭代数组而不重复,但我无法弄清楚我的生活如何跟踪每个团队的输赢将它们存储在我的数组中的正确位置。代码如下
for (x = 0; x < alCTeams.size() - 1; x++) {
for (y = x + 1; y < alCTeams.size(); y++) {
for (iGameCount = 0; iGameCount < 2; iGameCount++) {
do {
iHTScore = rndGenerator.nextInt(100);
iVTScore = rndGenerator.nextInt(100);
if (iGameCount % 2 == 0)
iHTScore += 5;
else
iVTScore += 5;
} while (iHTScore == iVTScore);
if (iHTScore > iVTScore) {
iHTWins++;
iVTLosses++;
} else {
iHTLosses++;
iVTWins++;
}
}
alCTeams.get(y).setWins(iVTWins);
alCTeams.get(y).setWins(iVTLosses);
}
alCTeams.get(x).setWins(iHTWins);
alCTeams.get(x).setLosses(iHTLosses);
}
答案 0 :(得分:0)
检查以下代码:我已添加了评论来描述。
for (x = 0; x < alCTeams.size()-1; x++) {
for (y = x+1; y < alCTeams.size(); y++) {
// Before game begins, reset the stats to zero
iHTWins = 0;
iVTLosses = 0;
iHTLosses = 0;
iVTWins = 0;
for (iGameCount = 0; iGameCount < 2; iGameCount++) {
do {
iHTScore = rndGenerator.nextInt(100);
iVTScore = rndGenerator.nextInt(100);
if (iGameCount % 2 == 0) {
iHTScore += 5;
} else {
iVTScore += 5;
}
} while (iHTScore == iVTScore);
if (iHTScore > iVTScore) {
iHTWins++;
iVTLosses++;
} else {
iHTLosses++;
iVTWins++;
}
}
alCTeams.get(y).setWins(alCTeams.get(y).getWins() + iVTWins);
alCTeams.get(y).setLosses(alCTeams.get(y).getLosses() + iVTLosses);
// Moved inside loop
alCTeams.get(x).setWins(alCTeams.get(x).getWins() + iHTWins);
alCTeams.get(x).setLosses(alCTeams.get(x).getLosses() + iHTLosses);
}
}
答案 1 :(得分:0)
您应该检索相应循环内每个团队的当前赢/输数量。否则你只是增加一些一般的赢利损失计数并将它们随机存储到团队中:
for (x = 0; x < alCTeams.size() - 1; x++) {
// retrieve current wins/losses for team x
iHTWins = alCTeams.get(x).getWins();
iHTLosses = alCTeams.get(x).getLosses();
for (y = x + 1; y < alCTeams.size(); y++) {
// retrieve current wins/losses for team y
iVTWins = alCTeams.get(y).getWins();
iVTLosses = alCTeams.get(y).getLosses();
for (iGameCount = 0; iGameCount < 2; iGameCount++) {
// do 2 game stuff
}
// store wins/losses for team y back to list
alCTeams.get(y).setWins(iVTWins);
alCTeams.get(y).setLosses(iVTLosses); // set losses, not wins
}
// store wins/losses for team x back to list
alCTeams.get(x).setWins(iHTWins);
alCTeams.get(x).setLosses(iHTLosses);
}