dimanche 27 novembre 2016

Score not adding up correctly in Java program [duplicate]

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.

Here's my code:

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[0]);
            for(int allrbA=0; allrbA<4; allrbA++) {
                int rbvalue = randoms1.nextInt(9);
                if (rbvalue== loadG4[0]) {
                    rbvalue=9;
                }
                selectrb[allrbA].setText(""+rbvalue);
            }
            selectrb[rbselector].setText(""+ loadG4[0]);

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

            if (score<4) {
                for (int allrbA = 0; allrbA < 4; allrbA++) {
                    final int rbselector = rbselection.nextInt(4);
                    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[0]) {
                                score++;
                                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 {
                                loadG4[0] = GenerateG4.nextInt(10);
                                number.setText("" + loadG4[0]);
                                for (int allrbA = 0; allrbA < 4; allrbA++) {
                                    int rbvalue = randoms1.nextInt(9);
                                    if (rbvalue == loadG4[0]) {
                                        rbvalue = 9;
                                    }
                                    selectrb[allrbA].setText("" + rbvalue);
                                }
                                final int rbselector = rbselection.nextInt(4);
                                selectrb[rbselector].setText("" + loadG4[0]);
                            }
                        }
                    });
                }
            }

My issue is that the score output is disordered, despite how many times the user clicks the button storing loadG4. This button and the value of loadG4 are constantly changing, as shown above. If I count what should be 6 points, I may get 2 points or 3 points, for example. The user gets awarded a point for the wrong reason, and I'm not sure what it is. 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.

I'd appreciate it if anyone could explain what the root of my issue is, and how it could be resolved. Many thanks in advance.

Additional summary (read full post first):

There are 4 buttons and 4 random numbers for 0-8 which are generated and put randomly in each button. rbvalue is put 4 times (random each time) into all 4 buttons. LoadG4 then replaces one rbvalue in a random button. If an rbvalue is equal to loadG4, it'll be set to 9 to ensure it's never the same as LOADG4. If the user clicks the button with LoadG4 in it, they get a point. They're meant to get a point whenever they click a button containing LoadG4, and that button is random each time.

Note: I uploaded a similar question already I'm aware, apologies but I had to repost as my last question was neglected. Would be grateful if someone could help me clear this up, thank you.




Aucun commentaire:

Enregistrer un commentaire