Advertisement
desislava_topuzakova

05. Login

May 21st, 2022
1,713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Login_05 {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         //1. username - текст (вход)
  7.         String username = scanner.nextLine();
  8.         //2. парола
  9.         String password = "";
  10.         //"Acer" -> последна до първа
  11.         for (int position = username.length() - 1; position >= 0 ; position--) {
  12.             char currentSymbol = username.charAt(position);
  13.             password += currentSymbol;
  14.         }
  15.         //3.
  16.         //повтаряме: въвеждаме парола
  17.         //стоп: въведената парола == password
  18.         //продължаваме: въведената парола != password
  19.         int countFailed = 0; //брой на грешно въведени пароли
  20.         String enteredPassword = scanner.nextLine(); //въведената парола
  21.  
  22.         while (!enteredPassword.equals(password)) {
  23.             //грешно въведена парола
  24.             countFailed++;
  25.             if (countFailed == 4) {
  26.                 System.out.printf("User %s blocked!", username);
  27.                 break;
  28.             }
  29.             System.out.println("Incorrect password. Try again.");
  30.             enteredPassword = scanner.nextLine();
  31.         }
  32.  
  33.         //enteredPassword == password
  34.         if (enteredPassword.equals(password)) {
  35.             System.out.printf("User %s logged in.", username);
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement