我写了乐透代码。
效果很好。
但是如果我包含四个以上的静态数字,
几乎需要20秒钟才能得出结果。 (它在Galaxy S9 +上运行。)
让我知道您是否可以更快地获得结果。
ArrayList<Integer> newNumbers = new ArrayList<>();//Define Lotto Scope
ArrayList<Integer> resultNumbers = new ArrayList<>(); //Only 6 numbers
ArrayList<Integer> includingNumbers = new ArrayList<>(); //Number to Include
ArrayList<TextView> textViews = new ArrayList<>(); //android textViews
startLotto();
public void startLotto(){
if(isStaticNumbers==true){//when static numbers is included
applyStaticNumbers();
while (applyStaticNumbers() == false) {
restart();
}
}
}
public boolean applyStaticNumbers() {
newNumbers.clear(); //for restart();
resultNumbers.clear();
for (int i = 1; i < 46; i++) {
newNumbers.add(i);
}
Collections.shuffle(newNumbers);
for (int i = 0; i < 6; i++) {
resultNumbers.add(newNumbers.get(i));
}
resultNumbers.sort(Comparator.naturalOrder()); //sort Numbers
if (resultNumbers.containsAll(includingNumbers)) {
for (int i = 0; i < 6; i++) {
textViews.get(i).setText(""+resultNumbers.get(i));
}
} else {//If not included, restart.
return false;
}
return true;
}
public void restart() {
applyStaticNumbers();
}