简体   繁体   English

单击正确的按钮后得分不会高于1? (Java-Android Studio)

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

In the program, I have two random values (loadG4 and rbvalue). 在程序中,我有两个随机值(loadG4和rbvalue)。 These values are set to 4 buttons, rbvalue to 3 and loadg4 to one (loadg4 overrides on of the rbvalue buttons). 这些值设置为4个按钮,rbvalue设置为3,loadg4设置为1(loadg4替代rbvalue按钮)。 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. 我的问题是,即使用户单击存储loadG4的按钮多少次,得分也永远不会高于1。 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. 我还注意到,如果按下包含非loadG4值的按钮,则将生成新值,并且按下保存新loadG4的下一个按钮,分数也不会上升。 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. 我的意图是使程序每次用户单击包含当前loadG4值的按钮时都添加一个点,这是您期望的。

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. 问题是您要在多个具有不同范围的位置中创建loadG4变量。 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 ). 您正在创建一个新变量G4,它与实例变量loafG4分开存在,实例变量loafG4首先创建(并且永远不会更改)。 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. 另外,您正在更改onClickListener内的按钮文本的值,这就是为什么按钮标签在按下时会不断变化的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM