I was doing an exercise for the university using the class Scanner
and something unusual happened, watch the code.
for(int i = 0; i < 11; i ++){
//problema(esta pulando a escolha de um dos jogadores, precisamente o jogador 1)
System.out.println(P[i]);
System.out.println("Jogador 1, faça a sua aposta:");
aposta1 = leitor.nextInt();
System.out.println("Jogador 2, faça a sua aposta:");
aposta2 = leitor.nextInt();
System.out.println("Jogador 1, insira a sua resposta:");
resposta1 = leitor.nextLine();
System.out.println("Jogador 2, insira a sua resposta:");
resposta2 = leitor.nextLine();
This code should accept the responses of both players, but check out the program:
Quanto é 2 + 2?
A- 1
B- 2
C- 3
D- 4
Jogador 1, faça a sua aposta:
10
Jogador 2, faça a sua aposta:
10
Jogador 1, insira a sua resposta:
Jogador 2, insira a sua resposta:
D
O jogador 1 errou
O Jogador 2 acertou
As you can see, the program skips the reader from resposta1
not allowing me to put a response, returning as empty. This type of problem only happened after I inserted the readers of aposta1
and aposta2
, when they are removed the readers of resposta1
and resposta2
are read normally.
As a workaround I created another class Scanner
for resposta1
and resposta2
separating the readers, which in the end worked. But I was curious as to why the error reader being used multiple times