在游戏

时间:2016-11-21 18:46:45

标签: java android random

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

这是我的代码:

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

我的问题是,无论用户点击存储loadG4的按钮多少次,分数输出都是无序的。此按钮和loadG4的值不断变化,如上所示。例如,如果我计算应该是6分,我可能得到2分或3分。用户因错误的原因获得了积分,我不确定它是什么。我的目的是让程序在每次用户点击按住当前loadG4值的按钮时添加一个点,就像你期望的那样。

如果有人能解释我的问题的根源是什么,以及如何解决,我会感激不尽。非常感谢提前。

0 个答案:

没有答案