-1

In a Prediction Game, two or more players attempt to predict the score of a series of upcoming sporting competitions. Each player's predictions are then evaluated based on the sum of the categories listed below. These descriptions use the following variables:

S1: Actual score earned by team 1.

S2: Actual score earned by team 2.

P1: A player's predicted score for team 1.

P2: A player's predicted score for team 2.

but I have exception java.lang.ArrayIndexOutOfBoundsException

public void game() {
    int s1, s2, p1, p2, winner, team1, team2, pointSpread, total = 0;

    int testCase = scan.nextInt();

    for (int k = 0; k < testCase; k++) {

        int p = scan.nextInt();
        int c = scan.nextInt();

        int[]result = new int[p];
        String[]pName = new String[p];

        for (int i = 0; i < p; i++) {
            pName[i] = scan.next();

            for (int j = 0; j < c; j++) {
                p1 = scan.nextInt();
                p2 = scan.nextInt();
                s1 = 20;
                s2 = 13;

                if (s1 > s2 && p1 > p2) {
                    winner = 10;
                }
                if (s1 < s2 && p1 < p2) {
                    winner = 10;
                } else {
                    winner = 0;
                }

                team1 = 5 - Math.abs(s1 - p1);
                team2 = 5 - Math.abs(s2 - p2);
                pointSpread = 5 - Math.abs(p1 - p2)-(s1 - s2);

                total += winner + team1 + team2 + pointSpread;
                result[i] = total;
            }
        }

        //compare players scores
        int max = result[0], playerNo = 0;
        for (int m = 1; m <= p; m++)//this line error
            if (max < result[m]) {
                max = result[m];
                playerNo = m;
            }

        System.out.println(pName[playerNo]);
    }
}
1

1 Answer 1

2

Change the error line to for(int m=1; m<p;m++)

Sign up to request clarification or add additional context in comments.

2 Comments

@Arc676 Please comment only after checking the facts. Index 0 is already handled by the user outside the loop.
Sorry. The comment was outside the default size of the code snipped and it looked like the code ended there (before the loop). My bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.