Я написал этот код:
while (choice > 2 && count <= 2) {
System.out.println("The number you have typed is invalid. Please try again.");
choice = s.nextInt();
count++;
}
if (choice == 1 && count <= 2) {
System.out.println("You have chosen teacher's information.");
t.information();
} else if (choice == 2 && count <= 2) {
System.out.println("You have chosen student's information.");
st.info();
} else {
System.out.println("You have been blocked.");
}
Это работает так, как я намеревался, если я попытаюсь более 2 раз, я буду заблокирован, однако, в этом другом проекте:
while (!rusern.equalsIgnoreCase(username) || !rpass.equals(password) && attempts <= 2) {
System.out.println("Login error. Your username is: " + rusern.equalsIgnoreCase(username) + " ,and password is: " + rpass.equals(password) + "\nPlease try again.");
System.out.println("Please enter your username:");
rusern = s.nextLine();
System.out.println("Enter your password:");
rpass = s.nextLine();
attempts++;
//here i have to use break;
}
if (rusern.equalsIgnoreCase(username) && rpass.equals(password) && attempts <= 2) {
System.out.println("You have succesfully logged in! Please continue: ");
} else {
System.out.println("You have been blocked.");
}
Я должен использовать перерыв; в цикле while, где я упоминал в комментарии, поскольку, если бы я не использовал перерыв, он отображал вопросы снова и снова, если я не отвечал правильно, без остановки, а когда я, наконец, отвечал правильно, меня блокировали. , Итак, мой вопрос: почему бы не работать без перерыва, как в первом проекте?
Обе переменные, «попытки» и «подсчет» и инициализированы одинаково, = 0.