2
I’m setting up a basic ATM simulator in Android Studio, where the user must enter the password, which is already pre-defined, in the case is "Java". If the password is correct, the message "Correct Password" appears, if it is wrong, the message "Incorrect Password" appears on the screen. If the user misses the password 3 times, a "Blocked Card" message will appear, and that’s basically it. But with my code, when the password is correct, the message appears right, already if the password is wrong it just shows "Blocked Card", without going through the message "Incorrect Password", I wanted to know if I did something wrong. Follows the code:
public class MainActivity extends AppCompatActivity {
EditText digito;
TextView res;
Button entrar;
int cont = 0;
String senha = "Java";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
digito = (EditText) findViewById(R.id.editText1);
entrar = (Button) findViewById(R.id.btnEntrar);
res = (TextView) findViewById(R.id.res);
}
public void Verificacao(View view) {
for (int i = 0; i < 4; i++) {
if (digito.getText().toString().equals(senha)) {
res.setText("Senha Correta");
break;
}else if (i == 3) {
res.setText("Cartão Bloqueado");
break;
} else {
res.setText("Senha Incorreta");
}
}
}
}
I referenced the method itself in the onClick XML property. And I just tested your idea and it worked, only the behavior didn’t turn out like I wanted, so I put the++ attempts inside the Else and not inside the if, and it worked correctly. And your hint about the method in uppercase letter I will start to have this habit.
– Leo
In fact, the
tentativas++
I was in the wrong place. I already edited the answer. If this answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.– Rafael Tavares