I'm making a pretty basic app with a screen that simulates a bingo card.
The problem is that when I run the code, without checking for repeated numbers (because there are no repeated numbers in a bingo card), the program runs quietly:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random nRand = new Random();
ArrayList<View> btn;
int[] sorteados = new int[25];
View cartela = findViewById(R.id.CartelaMain); //CartelaMain é o layout principal onde estão os 25 botões e um TextView
btn = cartela.getTouchables();
for(int cont = 0;cont < btn.size();cont++){
int numero = nRand.nextInt(99) + 1; //Definindo o numero randômico
Button botao = (Button) btn.get(contA); //Instanciando o botão
String txt1 = Integer.toString(numero); //Passando de int para String
botao.setText(txt1); //Set texto do botao
}
}
Now when I apply the verification:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random nRand = new Random();
ArrayList<View> btn;
int[] sorteados = new int[25];
View cartela = findViewById(R.id.CartelaMain); //CartelaMain é o layout principal onde estão os 25 botões e um TextView
btn = cartela.getTouchables();
for (int contA = 0; contA < btn.size(); contA++) { //Este método gera os numeros randomicametne e verifica por repetição dos numeros sorteados
int numero = nRand.nextInt(99) + 1;
Boolean continuar = true;
do {
Boolean sair = true;
for (int contB = 0; contB < btn.size(); contB++) {
int aux = sorteados[contB];
if(aux == numero){
numero = nRand.nextInt(99) + 1;
continuar = !sair;
contB = contB - 1;
}
}
} while (!continuar) ;
sorteados[contA] = numero; //Armazenamento do numero no Array
Button botao = (Button) btn.get(contA); //Instanciando o botão
String txt1 = Integer.toString(numero); //Passando de int para String
botao.setText(txt1); //Set texto do botão
}
}
The app simply does not leave the white screen. There is no record of compile errors by Android Studio and I have also debugged about 4 times.
I found the solution to the above situation, there was an infinite loop loop:
int number = nRand.nextInt (99) + 1; Boolean continue = true; of { Boolean exit = true; for (int contB = 0; contB When declared this way, the loop created for verification was not effective. Ex:
for (cont = 0; contd