2

For some reason only the final values in the array are assigned values...why is this?

public void openFrameScores() {
    int x = 0;
    int y = 0;
    int total = 0;

    for(int i = 0; i < framesToBowl; i++) {
        scores = new int[2][framesToBowl];
        x = (int)(Math.random() * 9);
        if(x == 0) y = (int)(Math.random() * 9);
        else y = (int)(Math.random() * (9 - x));
        scores[0][i] = x;
        scores[1][i] = y;
    }

    for(int i = 0; i < framesToBowl; i++) {
        total = total + scores[0][i] + scores[1][i];
        System.out.println("Frame: " + i + ", ball 1 = " + scores[0][i] +
        ", ball 2 = " + scores[1][i] + ", total score = " + total);
    }

}



------------------------------------------------

Frame: 0, ball 1 = 0, ball 2 = 0, total score = 0  
Frame: 1, ball 1 = 0, ball 2 = 0, total score = 0  
Frame: 2, ball 1 = 0, ball 2 = 0, total score = 0  
Frame: 3, ball 1 = 0, ball 2 = 0, total score = 0  
Frame: 4, ball 1 = 0, ball 2 = 0, total score = 0  
Frame: 5, ball 1 = 0, ball 2 = 0, total score = 0  
Frame: 6, ball 1 = 0, ball 2 = 0, total score = 0  
Frame: 7, ball 1 = 0, ball 2 = 0, total score = 0  
Frame: 8, ball 1 = 0, ball 2 = 0, total score = 0  
Frame: 9, ball 1 = 6, ball 2 = 1, total score = 7  
1
  • 1
    i too am having the same problem Commented Oct 23, 2012 at 5:30

3 Answers 3

7

Because at each iteration you're redeclaring the array.

for(int i = 0; i < framesToBowl; i++) {
        scores = new int[2][framesToBowl];   // Here!!!

At each iteration you say that scores receives a new, fully zeroed vector. That's why you can only see the value of the last iteration.

You can solve this problem by taking the initialization of scores outside of the loop.

scores = new int[2][framesToBowl];
for(int i = 0; i < framesToBowl; i++) {
    x = (int)(Math.random() * 9);
    if(x == 0) y = (int)(Math.random() * 9);
    else y = (int)(Math.random() * (9 - x));
    scores[0][i] = x;
    scores[1][i] = y;
}
Sign up to request clarification or add additional context in comments.

1 Comment

fix it by taking scores = new int[2][framesToBowl]; out of the loop
0

Take out the array initialization from the for loop.

public void openFrameScores() {
    int x = 0;
    int y = 0;
    int total = 0;
scores = new int[2][framesToBowl];
    for(int i = 0; i < framesToBowl; i++) {

        x = (int)(Math.random() * 9);
        if(x == 0) y = (int)(Math.random() * 9);
        else y = (int)(Math.random() * (9 - x));
        scores[0][i] = x;
        scores[1][i] = y;
    }

    for(int i = 0; i < framesToBowl; i++) {
        total = total + scores[0][i] + scores[1][i];
        System.out.println("Frame: " + i + ", ball 1 = " + scores[0][i] +
        ", ball 2 = " + scores[1][i] + ", total score = " + total);
    }

}

Comments

0

You reset your array at the beginning of the loop.

scores = new int[2][framesToBowl];

This keeps resetting the scores array. So when you go and read it at the bottom only the last instance of it is called.

Just declare it outside the for loop and that should fix your problem.

Comments

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.