正确点击按钮后得分不会高于1? (Java - Android Studio) [英] Score not going higher than one upon correct button click? (Java - Android Studio)

查看:117
本文介绍了正确点击按钮后得分不会高于1? (Java - Android Studio)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在程序中,我有两个随机值(loadG4和rbvalue)。这些值设置为4个按钮,rbvalue设置为3,loadg4设置为1(loadg4覆盖rbvalue按钮)。随机生成它们并随机分配给按钮。其他详细信息清楚地显示在下面的代码中。

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);
                            }
                        }
                    });
                }
            }

我的问题是得分永远不会高于1尽管用户点击存储loadG4的按钮多少次。我还注意到,如果按下包含不是loadG4的值的按钮,则会生成新的按钮,并按下保持新的loadG4的下一个按钮,分数也不会上升。我的目的是让程序在每次用户点击按住当前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.

推荐答案

问题是你在不同范围的多个地方创建了loadG4变量。当你这样做:

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);

您正在创建一个新变量G4,它与您首先创建的实例变量loafG4分开存在(和永远不会改变
)。因此,您总是与常量值进行比较。

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.

此外,您正在更改onClickListener中按钮文本的值,这就是为什么按钮标签在按下时不断变化的原因他们。

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

这篇关于正确点击按钮后得分不会高于1? (Java - Android Studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆