简体   繁体   中英

Index out of bound error varies when it shows

I keep getting an Index out of bounds error when i run this program periodically. I notice it more when I enter a higher starting percentage like 80+. I know it has to do with the shotsMade ArrayList . But i am not sure what to do to correct it. Thanks.

    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    

Not run the code, but it looks like if you run the game and get 10 In's, followed by a In on the second game then you never add a second entry to the array list..

Therefore the second game will throw an IndexOutOfBoundsException when you call

shotsMade.set(1, shotsMade.get(1)+1);

in the second game

When you add to shotsMade you will add another entry at the end. If you set you will change one position:

so

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

So if you want to call shotsMade.get(1) you need to miss once first because if you hit you use set and your list doesn't expand and is too short.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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