2

I have a game with progress chart and an array. I want to have a chart which the player can see its score in its last 5 games.

here's my code in my array

int[] Addition = { score1, score2, score3, score4, score5 };

if (score1 == 0) {
    score1 = Game.score;

} else if (score1 != 0 && score2 == 0) {
score2 = 21;
} else if (score2 != 0 && score3 == 0) {
score3 = Game.score;
} else if (score3 != 0 && score4 == 0) {
score4 = Game.score;
} else if (score4 != 0 && score5 == 0) {
score5 = Game.score;
}

What is the problem on my logic? when it runs my first game score seems to be right. but when i play one more its just that the 1st element of the array is changing? where Am I wrong? btw please apologize my english. and I appreciate any suggestions and comments. thanks guys

:::UPDATE:::

here's my code now. Can someone check if my initialization is correct:

public class ProgressGraph extends Activity {
int[] Addition = { 0, 0, 0, 0, 0 };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    openChart();
}

public void openChart() {

for (int i = 0; i < 5; i++) {
    if (Addition[i] == 0) {
        Addition[i] = Game.score;
        break;
    }
}
5
  • Not sure if I understood your question correctly, but it looks like you're expecting the 'if' block to hit multiple 'else if' branches, which is not like it works in Java. Commented Jan 13, 2015 at 13:51
  • Why are you addressing each element of the array individually? Use a loop and indexes instead. Commented Jan 13, 2015 at 13:52
  • sir I just want to make each element of my array to be the score of the player consecutively. Im having a hard time with this. Sir @Egor and sir barq can you please give me some blocks of codes that I can depend on and also to have a new learning? Commented Jan 13, 2015 at 14:00
  • Try static int[] Addition = { 0, 0, 0, 0, 0 }; That way it will only get called once. Commented Jan 13, 2015 at 16:50
  • that solves the problem ! thank you so much ! god bless programmers :) Commented Jan 13, 2015 at 16:56

4 Answers 4

3

I would do something like this:

for(int i = Addition.length-1; i > 0; i--){
    Addition[i] = Addition[i-1];
}
Addition[0] = Game.score;

This will mean that the most recent game will always be in position 0. If the user plays more than 5 games the oldest score gets replaced.

It also allows the user to be able to score 0.

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

1 Comment

Blast it, you beat me to it.
3

This part of code seems to be good. I think your score array is reset when you start the second game. Did you try to print the scores array before the end of the second game ? Does the first score remain stored ?

Then I suggest you to use a loop like that (not tested):

for (int i = 0; i < 5; i++) {
    if (score[i] == 0) {
        score[i] = Game.score;
        break;
    }
}

4 Comments

Sir yes it is printed. The first score was replaced by the second score which is not right. And also Id try your codes sir but it seems that its still same result as what I had done on if block statement.
The condition is strict and clear. So I think that the mistake is elsewhere.
I would assume that declaring your Addition array in the method as opposed to the class if Maxime's answer is not working
sir what do you mean by opposed to the class?
2

Are you trying to move each old score down the list?

for (int i = 4; i > 0; i--) {
    Addition[i] = Addition[i-1];
}

Addition[0] = Game.score;

In these code samples we've provided, the array values should be initialized to zero:

int[] Addition = { 0, 0, 0, 0, 0};

4 Comments

I have a chart that the player see his scores progress in his last 5 games sir
Do you want new scores at the bottom (use Maxime's code), or at the top (use code Chris Handy and I posted)? You may have something else going on, but, this type of code should work.
should it remain? int[] Addition = { score1, score2, score3, score4, score5 };
No, I was just adding a note that the array values should be initialized to 0.
0

Taking into account your update. - When you declare the array. The variable has a capital letter so it may have a conflict with a class. Replace Addition by addition.

With this code, if you start another activity, scores will be reset. You have to use Application extended class or SharedPreferences to save scores.

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.