desislava_topuzakova

05. Login

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