了解从数组列表中删除项目背后的逻辑

时间:2019-04-23 02:13:32

标签: java arraylist

所以,这是我的第一个问题,如果无法正确格式化,我深表歉意,但是基本上,我的任务是制作一个比赛类的比赛程序,该程序将淘汰存储在数组列表中的8支球队,直到三轮后剩下一个。老实说,大多数逻辑看起来都是正确和安静的,我只是在这个逻辑上碰壁了。

我尝试了从数组中删除索引的不同方法,并且每次都遇到错误。挖掘之后,我能够找到一些逻辑,使我可以在某种程度上运行程序而不会返回错误。但是我相信我只是从根本上错过了一些东西。我能做的任何见解将不胜感激。

import java.util.ArrayList;
import java.util.Random;

public class QuidditchSim {

    public static void main(String[] args) {
        ArrayList<String> teams = new ArrayList<>();
        teams.add("The Nomads");
        teams.add("The Cabbages");
        teams.add("The Sparklers");
        teams.add("The Turtles");
        teams.add("The EarthMovers");
        teams.add("The WaterBoys");
        teams.add("The FireMen");
        teams.add("The Noodles");

        int round = 1;
        for (int o = 0; o < 4; o++) {
            System.out.println("Round # " + round);
            System.out.println("=============================================");
            round++;

            for (int i = 0; i < teams.size(); i = i + 1) {
                playGame(i, i + 1, teams);

            }

            teams.clear();
        }

    }

    public static void playGame(int TeamA, int TeamB, ArrayList<String> teams) {
        int scoreA = 0;
        int scoreB = 0;
        int totalA = 0;
        int totalB = 0;
        boolean gotSnitch = false;
        int quarterNumber = 1;
        Random rand = new Random();
        System.out.println(teams.get(TeamA) + " vs " + teams.get(TeamB));

        do {
            if (gotSnitch() == true) {
                int random = rand.nextInt(100);
                if (random % 2 == 0) {
                    System.out.println(teams.get(TeamA) + " are the winner! The snitch was caught.");
                    teams.remove((TeamB));
                    System.out.println("");
                    return;
                } else {
                    System.out.println(teams.get(TeamB) + " are the winner! The snitch was caught.");
                    teams.remove((TeamA));
                    System.out.println("");
                    return;
                }
            }
            scoreA = randomScore();
            scoreB = randomScore();
            System.out.println("Qtr " + quarterNumber + ":  " + scoreA + " \t" + scoreB);
            quarterNumber++;
            totalA += scoreA;
            totalB += scoreB;
        } while (totalA == totalB || quarterNumber <= 4);
        {
            if (totalA > totalB) {
                System.out.println(teams.get(TeamA) + " Win " + totalA + " to " + totalB + "! " + teams.get(TeamB) + " eliminated");
                System.out.println("");
                teams.remove((TeamB));
                return;
            } else {
                System.out.println(teams.get(TeamB) + " Win " + totalB + " to " + totalA + "! " + teams.get(TeamA) + " eliminated");
                System.out.println("");
                teams.remove((TeamA));
                return;
            }

        }
    }

    public static boolean gotSnitch() {
        Random rand = new Random();
        int random = rand.nextInt(100);
        if (random > 15) {
            return false;
        } else {
            return true;
        }
    }

    public static int randomScore() {
        Random rand = new Random();
        int random = rand.nextInt(4);
        random = random * 10;
        return random;
    }
}

1 个答案:

答案 0 :(得分:0)

我看不到您的逻辑有什么不对,只有一部分。这里的for循环:

for (int i = 0; i < teams.size(); i = i + 1) {
        playGame(i, i + 1, teams);

}

此中的i变量将超出数组范围,因为在代码的这一部分

playGame(i, i + 1, teams);尝试访问数组+ 1的最后一个索引。因此,您的for如下所示:

for (int i = 0; i < teams.size() - 1; i = i + 1) {
        playGame(i, i + 1, teams);

}

teams.size() - 1将阻止代码访问超出数组范围的索引