简体   繁体   English

如何使用嵌套循环在arraylist中存储值

[英]How to store values in an arraylist using nested loops

I'm writing a fantasy basketball team program in Netbeans where 10 teams play 2 games against each team and I'm trying to keep track of each team's number of wins and losses. 我正在Netbeans写一个梦幻篮球队计划,其中10支球队对阵每支球队打2场比赛,我试图跟踪每支球队的输赢数量。 The ten teams are stored to an array list, and I have two loops set to iterate through the array without repeats, but I can't figure out for the life of me to how to keep track of the wins and losses for each team and store them in the correct place in my array. 十个团队被存储到一个数组列表中,我有两个循环设置迭代数组而不重复,但我无法弄清楚我的生活如何跟踪每个团队的输赢将它们存储在我的数组中的正确位置。 Code is as follows 代码如下

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);
}

Check below code: I've added comments to describe. 检查下面的代码:我添加了评论来描述。

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);
        }
    }

You should retrieve the current number of wins/losses for each team inside the respective loops. 您应该检索相应循环内每个团队的当前赢/输数量。 Otherwise you are just increasing some general win-loss-counts and store them randomly to teams: 否则你只是增加一些一般的赢利损失计数并将它们随机存储到团队中:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM