【发布时间】:2021-02-22 21:42:07
【问题描述】:
我的 Activity 中连续有 4 个 Button 元素。我有四个统计值,我想在每次按下按钮后将这些值随机分配给按钮。
目前我编写了一个生成器方法,它生成 4 个不同的值并将按钮值设置为它们。我想知道,也许在 Android 中有更有效的工具来完成这项任务?
生成器:
public void generator(View view) {
int one = new Random().nextInt(4);
int two;
do {
two = new Random().nextInt(4);
} while (two == one);
int three;
do {
three = new Random().nextInt(4);
} while (three == one || three == two);
int four;
do {
four = new Random().nextInt(4);
} while (four == one || four == two || four == three);
buttonOnebutton.setText(String.valueOf(one));
buttonTwobutton.setText(String.valueOf(two));
buttonThreebutton.setText(String.valueOf(three));
buttonFourbutton.setText(String.valueOf(four));
}
【问题讨论】: