Random number generator issue - Android Studio

2019-09-15 02:33发布

问题:

I have a random number (loadG1) that is outputted after the user presses enter on an EditText. I added a few if statements near the end of the timer to ensure that a number of specific digit length is outputted depending on the user's score. They get a point added each time their input matches loadG1. My problem is that sometimes on the outputted randoms before 3 points (>=0), I get a six digit number. This is only meant to happen when they have 3 or more points. This also happens at other stages, e.g: 7 digits meant to be outputted by 8 are instead. The pattern seems to almost work perfectly, but at times one digit more than the limit is outputted for that user's score. Never one digit less, but either the right amount or sometimes one more.

Here's the code:

final Random generateG1 = new Random();
            final int loadG1 = generateG1.nextInt(99999)+10000;
            final TextView number = (TextView) findViewById(R.id.number);
            number.setText(""+loadG1);

            final CountDownTimer loop = new CountDownTimer(3500, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                }
                @Override
                public void onFinish() {
                    number.setVisibility(View.GONE);
                    final TextView prompt = (TextView) findViewById(R.id.prompt);
                    prompt.setVisibility(View.VISIBLE);
                    prompt.setText(" Enter the number");
                    final EditText input = (EditText) findViewById(R.id.enterAnswer);
                    input.setVisibility(View.VISIBLE);
                    input.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                switch (keyCode) {
                                    case KeyEvent.KEYCODE_ENTER:
                                        Editable answer = input.getText();
                                        int finalAnswer = Integer.parseInt(String.valueOf(answer));
                                        int finalLoadG1 = Integer.parseInt(String.valueOf(number.getText()));
                                        input.setVisibility(View.GONE);
                                        prompt.setVisibility(View.GONE);
                                        if (finalAnswer == finalLoadG1) {
                                            score++;
                                        }

                                        number.setVisibility(View.VISIBLE);

                                        if (score>=0) {
                                            int loadG1 = generateG1.nextInt(99999)+10000;
                                            number.setText(""+loadG1);
                                        }
                                        if (score>=3){
                                            int loadG1 = generateG1.nextInt(999999)+100000;
                                            number.setText(""+loadG1);
                                        }
                                        if (score>=6){
                                            int loadG1 = generateG1.nextInt(9999999)+1000000;
                                            number.setText(""+loadG1);
                                        }
                                        if (score>=10){
                                            int loadG1 = generateG1.nextInt(99999999)+10000000;
                                            number.setText(""+loadG1);
                                        }

                                        input.getText().clear();

                                        start();

Would greatly appreciate it if someone could tell me what's causing this error, and how to resolve it. Thanks in advance.

回答1:

You need to change the conditions.

  1. Either reverse the order of if statements , or

  2. Add < condition also to if statements.

And make use of else also.

        if (score>=10){
            int loadG1 = generateG1.nextInt(99999999)+10000000;
            number.setText(""+loadG1);
        }
        else if (score>=6){
            int loadG1 = generateG1.nextInt(9999999)+1000000;
            number.setText(""+loadG1);
        }
        else if (score>=3){
            int loadG1 = generateG1.nextInt(999999)+100000;
            number.setText(""+loadG1);
        }else if (score>=0) {
            int loadG1 = generateG1.nextInt(99999)+10000;
            number.setText(""+loadG1);
        }

or

if (score>=0 && score<3) {
            int loadG1 = generateG1.nextInt(99999)+10000;
            number.setText(""+loadG1);
        }
        else if (score>=3 && score<6){
            int loadG1 = generateG1.nextInt(999999)+100000;
            number.setText(""+loadG1);
        }
        else if (score>=6 && score<10){
            int loadG1 = generateG1.nextInt(9999999)+1000000;
            number.setText(""+loadG1);
        }
        else if (score>=10){
            int loadG1 = generateG1.nextInt(99999999)+10000000;
            number.setText(""+loadG1);
        }