@Override
public void onClick(View v) {
List<Integer> list = new LinkedList<Integer>();
for (int i = 1; i <= 10; i++) {
list.add(i);
}
long seed = System.nanoTime();
Collections.shuffle(list, new Random(seed));
int number1 = list.remove(0);
int number2 = list.remove(0);
int number3 = list.remove(0);
int number4 = list.remove(0);
int number5 = list.remove(0);
int number6 = list.remove(0);
math.setText(number1 + "+" + number2);
buttona.setText("" + (number1 + number2));
buttonb.setText("" + (number3 + number4));
buttonc.setText("" + (number5 + number6));
}
我有这个代码,1到10之间有6个不同的数字,但我的问题是,我需要按钮文本会有所不同,我的代码不会这样做,例如:
Number1 = 3,Number2 = 4,(7)..
Number3 = 5,Number4 = 2,(7)..
如何使所有文本都不同?
答案 0 :(得分:1)
您可以尝试将随机值设置为shuffle
函数。随机添加一个种子,输出可能会有所不同:)。
尝试:
long seed = System.nanoTime();
Collections.shuffle(list, new Random(seed));