索引超出范围的错误会在显示时发生变化

时间:2014-03-28 07:06:42

标签: java arrays

当我定期运行此程序时,我不断收到Index out of bounds错误。当我输入更高的起始百分比(如80+)时,我会注意到更多。我知道它与shotsMade ArrayList有关。但我不知道该怎么做才能纠正它。感谢。

    import java.util.*;

    public class Test {
    public static void main(String[] args) {
        int myGameCounter = 1;  
        List<Integer> shotsMade = new ArrayList<Integer>();
        shotsMade.add(0);
        System.out.print("Enter Player's Free Throw Percentage: ");
        Scanner input = new Scanner(System.in);
        int percent = input.nextInt();


        //Game #1
        System.out.println("Game " + myGameCounter + ":");
        Random r = new Random();
        myGameCounter++;
        for (int i = 0; i < 10; ++i){
            boolean in = tryFreeThrow(percent);
            if (in) {
            shotsMade.set(0, shotsMade.get(0)+1);
            System.out.print("In" + " ");
            }
            else {
            shotsMade.add(0);
            System.out.print("Out" + " ");
            }
        }
        System.out.println("");
        System.out.println("Free throws made: " + shotsMade.get(0) + " out of 10");
            //Game #2
        System.out.println("");
        System.out.println("Game" + myGameCounter + ":");
        myGameCounter++;
        for (int i = 0; i < 10; ++i){
            boolean in = tryFreeThrow(percent);
            if (in) {
            shotsMade.set(1, shotsMade.get(1)+1);
            System.out.print("In" + " ");
            }
            else {
            shotsMade.add(0);
            System.out.print("Out" + " ");
            }   
        }
        System.out.println("");
        System.out.println("Free throws made: " + shotsMade.get(1) + " out of 10");
            //Game #3
        System.out.println("");
        System.out.println("Game" + myGameCounter + ":");
        myGameCounter++;
        for (int i = 0; i < 10; ++i){
            boolean in = tryFreeThrow(percent);
            if (in) {
            shotsMade.set(2, shotsMade.get(2)+1);
            System.out.print("In" + " ");
            }
            else {
            shotsMade.add(0);
            System.out.print("Out" + " ");
            }   
        }
        System.out.println("");
        System.out.println("Free throws made: " + shotsMade.get(2) + " out of 10");
            //Game #4
        System.out.println("");
        System.out.println("Game" + myGameCounter + ":");
        myGameCounter++;
        for (int i = 0; i < 10; ++i){
            boolean in = tryFreeThrow(percent);
            if (in) {
            shotsMade.set(3, shotsMade.get(3)+1);
            System.out.print("In" + " ");
            }
            else {
            shotsMade.add(0);
            System.out.print("Out" + " ");
            }   
        }
        System.out.println("");
        System.out.println("Free throws made: " + shotsMade.get(3) + " out of 10");
            //Game #5
        System.out.println("");
        System.out.println("Game" + myGameCounter + ":");
        myGameCounter++;
        for (int i = 0; i < 10; ++i){
            boolean in = tryFreeThrow(percent);
            if (in) {
            shotsMade.set(4, shotsMade.get(4)+1);
            System.out.print("In" + " ");
            }
            else {
            shotsMade.add(0);
            System.out.print("Out" + " ");
            }   
        }
        System.out.println("");
        System.out.println("Free throws made: " + shotsMade.get(4) + " out of 10");

        System.out.println("");
        System.out.println("Summary:");


  }//main        



    public static boolean tryFreeThrow(int percent) {
        Random r = new Random();
        int number = r.nextInt(100);
        if (number > percent){ 
         return false;
        }
        return true;
    }






}//class    

2 个答案:

答案 0 :(得分:0)

不运行代码,但看起来如果你运行游戏并获得10个In,然后在第二个游戏中输入In,那么你永远不会在数组列表中添加第二个条目..

因此,当您调用

时,第二个游戏将抛出IndexOutOfBoundsException
shotsMade.set(1, shotsMade.get(1)+1);

在第二场比赛中

答案 1 :(得分:0)

当你add到shotMade时,你会在最后添加另一个条目。如果您set,您将更改一个职位:

所以

0 1 2 3 -> add(0) -> 0 1 2 3 0
0 1 2 3 -> set(2, 0) -> 0 1 0 3

因此,如果您想要致电shotsMade.get(1),首先需要错过,因为如果您点击,则使用set并且您的列表不会展开并且太短。