dimanche 27 novembre 2016

Why does Java program flaw after first click? (Android Studio)

I have a program in which a random value is applied to four random buttons. One of the random values id displayed on top of the four buttons. If the user taps the button containing that number displayed (loadG4) they get a point. The problem I have with this is that once the user clicks the correct button and new numbers are generated, the rbvalue buttons may not all be different to the loadG4 value. As seen in my code, they should never be equal. Here's the code:

int score = 0;
Random random = new Random();
int rbselector = random.nextInt(4);    //These four initiations are out onCreate.
int loadG4 = random.nextInt(10);


final Button[] selectrb = new Button[4];
    selectrb[0] = rb1;
    selectrb[1] = rb2;
    selectrb[2] = rb3;
    selectrb[3] = rb4;


    final Button loseStarter4 = (Button) findViewById(R.id.Starter4);
    loseStarter4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            infoG4.setVisibility(View.GONE);
            loseStarter4.setVisibility(View.GONE);
            rb1.setVisibility(View.VISIBLE);
            rb2.setVisibility(View.VISIBLE);
            rb3.setVisibility(View.VISIBLE);
            rb4.setVisibility(View.VISIBLE);


            rbselector = random.nextInt(4);

            final TextView number = (TextView) findViewById(R.id.number);
            number.setText(""+ loadG4);


            for(int allrbA=0; allrbA<4; allrbA++) {
                int rbvalue = random.nextInt(9);
                if (rbvalue == loadG4) {
                    rbvalue=9;
                }
                selectrb[allrbA].setText(""+rbvalue);
            }
            selectrb[rbselector].setText(""+ loadG4);


                for (int allrbA = 0; allrbA < 4; allrbA++) {
                selectrb[allrbA].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            Button clicked = (Button) v;
                            String clickVal = (String) clicked.getText();
                            int finalClick = Integer.valueOf(clickVal);

                            if (finalClick == loadG4) {
                                score++;
                                for (int allrbA = 0; allrbA < 4; allrbA++) {
                                    int rbvalue = random.nextInt(9);
                                    if (rbvalue == loadG4) {
                                        rbvalue=9;
                                    }
                                    selectrb[allrbA].setText("" + rbvalue);
                                }
                                int loadG4 = random.nextInt(10);
                                number.setText("" + loadG4);
                                int rbselector = random.nextInt(4);
                                selectrb[rbselector].setText("" + loadG4);
                            }
                    }
                });
                }

I have it so that if an rbvalue is equal to loadG4, it is set to = 9. For some reason, after the first correct click it is possible that an rbvalue could be equal to loadG4.

Furthermore, the value of loadG4 never changes here either. Why is this?

Would be grateful to anyone who could look through this and explain why a) rbvalue are not all always different to loadG4 after first correct click and b) why loadG4 stays the same number.

Many thanks in advance.




Aucun commentaire:

Enregistrer un commentaire