gpt4 book ai didi

java - 如何在4秒内定时生成不同的随机数?

转载 作者:行者123 更新时间:2023-12-02 01:43:33 25 4
gpt4 key购买 nike

我正在尝试编写宾果游戏代码。我想生成 70 个每 4 秒改变一次的随机数。

我创建了一个倒计时器,并通过在倒计时器的芬兰语部分定义一个可运行对象,在 4 秒内创建了一个随机数。但这些随机数可能彼此没有不同。

Handler handler;
Runnable run;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Random random = new Random();
int i = random.nextInt(70);

Random random2 = new Random();
int z = random2.nextInt(70);
int y =random2.nextInt(70);
int x =random2.nextInt(70);
int w =random2.nextInt(70);
int q =random2.nextInt(70);
int v =random2.nextInt(70);
int m =random2.nextInt(70);
int n =random2.nextInt(70);
int l =random2.nextInt(70);


final TextView textView3 = (TextView) findViewById(R.id.textView3);
final TextView textView4 = (TextView) findViewById(R.id.textView4);
final TextView textView5 = (TextView) findViewById(R.id.textView5);
final TextView textView6 = (TextView) findViewById(R.id.textView6);
final TextView textView7 = (TextView) findViewById(R.id.textView7);
final TextView textView8 = (TextView) findViewById(R.id.textView8);
final TextView textView9 = (TextView) findViewById(R.id.textView9);
final TextView textView10 = (TextView) findViewById(R.id.textView10);



final TextView textView2 = (TextView) findViewById(R.id.textView2);
textView2.setText("Lucky number: " + i);

final int a = Integer.parseInt(textView3.getText().toString());
final int b = Integer.parseInt(textView4.getText().toString());
final int c = Integer.parseInt(textView5.getText().toString());
final int d = Integer.parseInt(textView6.getText().toString());
final int e = Integer.parseInt(textView7.getText().toString());
final int f = Integer.parseInt(textView8.getText().toString());
final int g = Integer.parseInt(textView9.getText().toString());
final int h = Integer.parseInt(textView10.getText().toString());

textView9.setText("" + z);
textView10.setText(" "+ y);
textView8.setText(" "+ x);
textView7.setText(" "+ w);
textView6.setText(" "+ q);
textView5.setText(" "+ l);
textView4.setText(" "+ m);
textView3.setText(" "+ n);


CountDownTimer ct0 = new CountDownTimer(60000, 1000) {


@Override
public void onTick(long millisUntilFinished) {


TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Remaining time: " + millisUntilFinished / 1000);

}

@Override
public void onFinish() {


}
}.start();

CountDownTimer ct1 = new CountDownTimer(4000, 1000) {


@Override
public void onTick(long millisUntilFinished) {

}

@Override
public void onFinish() {


handler = new Handler();
run = new Runnable() {
@Override
public void run() {

int[] numbers = new int[70];
Random random = new Random();
int i = random.nextInt(70);

TextView textView2 = (TextView) findViewById(R.id.textView2);
textView2.setText("Lucky number: " + i);

if ( i == a) {

textView3.setText("ok");
} else if (i == b) {
textView4.setText("ok");
} else if (i == c) {

textView5.setText("ok");
} else if (i == d) {
textView6.setText("ok");

} else if (i == e) {
textView7.setText("ok");
} else if(i == f) {
textView8.setText("ok");
} else if (i == g) {
textView9.setText("ok");
} else if (i == h) {
textView10.setText("ok");
}



handler.postDelayed(this, 4000);

}
};

handler.post(run);



}
}.

start();

}

在 60 秒内,我想每 4 秒创建 70 个不同的数字,并将这些数字与其他 8 个变量进行匹配。如果您能提供帮助,我将不胜感激。谢谢。

最佳答案

我可以给你一个方法来做到这一点,以避免你所说的:

But these random numbers may not be different from each other.

这是显而易见的,因为你无法控制它。

我想说创建一个 Set<Integer>你的大小是 70,所以你可以通过简单的 for 来填充这个数组循环。

for (int i = 0; i<70; i++) yourSetList.add(i);

然后“模拟”宾果游戏的一个好方法是对列表进行洗牌,因此您可以使用 Collections shuffle

然后你可以像这样生成一个随机数:

Random random = new Random();   
int random = random.nextInt(yourSetList.size()) + 1;

然后使用随机作为索引

yourSetList.get(random);

请确保将其从列表中删除,以避免出现最初的问题

yourSetList.remove(random);

关于java - 如何在4秒内定时生成不同的随机数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54080358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com