我正在尝试编写宾果游戏代码。我想产生70个随机数字,每4秒更改一次。
我创建了一个countdowntimer并通过在countdowntimer的finish部分中定义一个可运行项,在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个变量进行匹配。如果您能提供帮助,我将不胜感激。谢谢。
答案 0 :(得分:2)
为了避免您在说什么,我可以给您一种方法:
但是这些随机数可能互不相同。
这很明显,因为您无法控制它。
我想创建一个大小为70的Set<Integer>
,因此您可以通过简单的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);