简体   繁体   中英

Score not going higher than one upon correct button click? (Java - Android Studio)

In the program, I have two random values (loadG4 and rbvalue). These values are set to 4 buttons, rbvalue to 3 and loadg4 to one (loadg4 overrides on of the rbvalue buttons). They are randomly generated and assigned to a button by random. Any other details are clearly shown in the code below.

final Random rbselection = new Random();
    final int rbselector = rbselection.nextInt(4);
    final Button[] selectrb = new Button[4];
    selectrb[0] = rb1;
    selectrb[1] = rb2;
    selectrb[2] = rb3;
    selectrb[3] = rb4;

int score = 0;
final Random GenerateG4 = new Random();
            final int loadG4 = GenerateG4.nextInt(10);
            final Random randoms1 = new Random();
            final TextView number = (TextView) findViewById(R.id.number);
            number.setText(""+loadG4);
            for(int allrbA=0; allrbA<4; allrbA++) {
                int rbvalue = randoms1.nextInt(9);
                if (rbvalue==loadG4) {
                    rbvalue=9;
                }
                selectrb[allrbA].setText(""+rbvalue);
            }
            selectrb[rbselector].setText(""+loadG4);

            if (score<4) {
                for (int allrbA = 0; allrbA < 4; allrbA++) {
                    selectrb[allrbA].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            final int loadG4 = GenerateG4.nextInt(10);
                            number.setText("" + loadG4);
                            for (int allrbA = 0; allrbA < 4; allrbA++) {
                                int rbvalue = randoms1.nextInt(9);
                                if (rbvalue == loadG4) {
                                    rbvalue = 9;
                                }
                                selectrb[allrbA].setText("" + rbvalue);
                            }
                            final int rbselector = rbselection.nextInt(4);
                            selectrb[rbselector].setText("" + loadG4);
                        }
                    });
                }
            }

            if (score<4) {
                for (int allrbA = 0; allrbA < 4; allrbA++) {
                    selectrb[rbselector].setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String getrbvalue = (String) selectrb[rbselector].getText();
                            int finalisesrbvalue = Integer.valueOf(getrbvalue);
                            if (finalisesrbvalue == loadG4) {
                                score++;
                                final int loadG4 = GenerateG4.nextInt(10);
                                number.setText("" + loadG4);
                                for (int allrbA = 0; allrbA < 4; allrbA++) {
                                    int rbvalue = randoms1.nextInt(9);
                                    if (rbvalue == loadG4) {
                                        rbvalue = 9;
                                    }
                                    selectrb[allrbA].setText("" + rbvalue);
                                }
                                final int rbselector = rbselection.nextInt(4);
                                selectrb[rbselector].setText("" + loadG4);
                            }

                            else {
                                final int loadG4 = GenerateG4.nextInt(10);
                                number.setText("" + loadG4);
                                for (int allrbA = 0; allrbA < 4; allrbA++) {
                                    int rbvalue = randoms1.nextInt(9);
                                    if (rbvalue == loadG4) {
                                        rbvalue = 9;
                                    }
                                    selectrb[allrbA].setText("" + rbvalue);
                                }
                                final int rbselector = rbselection.nextInt(4);
                                selectrb[rbselector].setText("" + loadG4);
                            }
                        }
                    });
                }
            }

My issue is that the score never goes higher than one, despite how many times the user clicks the button storing loadG4. I also noticed that if a button containing value that isn't loadG4 is pressed then new ones are generated and the next button to hold the new loadG4 is pressed, the score doesn't go up there either. My intention is for the program to add a point each time the user clicks the button holding the current loadG4 value, as you'd expect.

Many thanks in advance to anyone able to help.

The issue is that you are creating the loadG4 variable in multiple places with different scopes. When you do this :

final int loadG4 = GenerateG4.nextInt(10);

You are creating a new variable G4 which exists separately from your instance variable loafG4 which was created first (and never changes ). Hence, you are always comparing against a constant value.

Also, you are changing the value of the button text inside onClickListener, that is why your button labels keep changing on pressing them.

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