Advertisement
desislava_topuzakova

05. Login

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