lakeviking lakeviking - 24 days ago 8
Java Question

How to increase a variable after every JButton click?

I am working on a program that simulates a soccer match. I have a button, which randomly one of the two teams will give a point when I click on it. So, after one click, the score could be 1-0 or 0-1, and after two cliks 2-0, 0-2 or 1-1.

The problem is that, every time I click, it is 0-1 or 1-0. So the score does not count on.

I know that the reason is that I said

int homescore = 0
and
int awayscore = 0
in the 'action performed' code. And after this 0-0 it gives randomly one of the teams 1 point. So when I click for the second time on the button, the score will start at 0-0 again, not on the new score.

Does someone know how I can solve this problem? Maybe it's very easy, but I haven't programmed often.

This is the part of the code I'm talking about:

private void resultbuttonActionPerformed(java.awt.event.ActionEvent evt) {
Random dice = new Random();
int number;
int homescore = 0;
int awayscore = 0;
for (int counter =1; counter<=1; counter++){
number = dice.nextInt(2); // Creates a 0 or a 1
if (number == 1){ // If 1: Point to home team
homescore = ++homescore;}
else { // If 0: Point to away team
awayscore = ++awayscore;
}
}


homescorelabel.setText(String.valueOf(homescore));
awayscorelabel.setText(String.valueOf(awayscore));

Answer

As you have explained, having

int homescore = 0; 
int awayscore = 0;

in your action performed will always increment one value starting from zero. What you need to do is get the existing value and increment one to it. There are two simple ways of doing it.

  1. Set a field variable to store the homescore and awayscore and increment from it.

    class GameClass { 
        private int homescore = 0;
        private int awayscore = 0;
    
        private void resultbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                              
            Random dice = new Random();
            int number;
            for (int counter =1; counter<=1; counter++){
                number = dice.nextInt(2);                   // Creates a 0 or a 1
                if (number == 1){                           // If 1: Point to home team
                    homescore = ++homescore;
                } else {                                      // If 0: Point to away team
                    awayscore = ++awayscore;
                }
            }
        homescorelabel.setText(String.valueOf(homescore));
        awayscorelabel.setText(String.valueOf(awayscore));
     }
    }
    

Note that I have just shown the class here (GameClass). When you increment one of the scores you are incrementing the field so that it will be preserved for each button click.

  1. Get the current score from the Label.

    private void resultbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                              
        Random dice = new Random();
        int number;
        int homescore = Integer.parseInt(homescorelabel.getText());
        int awayscore = Integer.parseInt(awayscorelabel.getText());
        for (int counter =1; counter<=1; counter++){
            number = dice.nextInt(2);                   // Creates a 0 or a 1
            if (number == 1){                           // If 1: Point to home team
                homescore = ++homescore;
            } else {                                      // If 0: Point to away team
                awayscore = ++awayscore;
            }
        }
        homescorelabel.setText(String.valueOf(homescore));
        awayscorelabel.setText(String.valueOf(awayscore));
    }
    
Comments