有时,在显示正确数量的数字后会出现一个数字

时间:2018-11-01 16:06:55

标签: java android

我有一个应用程序,每472毫秒显示5次随机数。每件事都起作用,但有时似乎是随机的,第六个数字出现的时间少于472毫秒,我不知道为什么。我将计数设置为6,以便第6个数字不出现并清除屏幕。

我的Java:

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;


public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
@SuppressLint("CheckResult")
public void display_numbers(View view) {
    final List<Integer> randomNumbers = new ArrayList<>();
    Observable.intervalRange(0, 6, 2000, 472, TimeUnit.MILLISECONDS)
            .map(aLong -> {
                int previous = randomNumbers.size() > 0 ? randomNumbers.get(
                        randomNumbers.size() - 1) : -1;
                int randomNumber = generateRandomNumber();
                while (randomNumber == previous) randomNumber = 
generateRandomNumber();
                randomNumbers.add(randomNumber);
                return randomNumber;
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(integer -> {
                TextView tv = (TextView) findViewById(R.id.textView1);
                tv.setText(String.valueOf(integer));
            }, error -> {}, () -> {
                TextView tv = (TextView) findViewById(R.id.textView1);
                tv.setText("");
            });
}
private int generateRandomNumber() {
        Random r = new Random();
        int minNumber = 1;
        int maxNumber = 9;
        return r.nextInt((maxNumber - minNumber) + 1) + minNumber;
}
    }

0 个答案:

没有答案