Advertisement
desislava_topuzakova

05. Login

Sep 17th, 2022
1,659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package basicSyntax.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.         //2. password -> username на обратно
  10.         //3. въвеждане на парола
  11.  
  12.         String username = scanner.nextLine(); //"Acer"
  13.         String password = ""; //парола -> "recA"
  14.         //всяка позиция в текста -> последната(дължина - 1) към първата(0)
  15.         for (int position = username.length() - 1; position >= 0; position--) {
  16.             char currentSymbol = username.charAt(position);
  17.             password += currentSymbol;
  18.         }
  19.         //знаем каква ни е паролата
  20.  
  21.         String enteredPassword = scanner.nextLine();
  22.         int countFailedTry = 0; //брой неуспешни опити за въвеждане на парола
  23.         //повтаряме: въвеждаме някаква парола
  24.         //стоп: enteredPassword == password
  25.         //продължаваме: enteredPassword != password
  26.         while (!enteredPassword.equals(password)) {
  27.             //грешно въведена парола
  28.             countFailedTry++;
  29.             if (countFailedTry == 4) {
  30.                 System.out.printf("User %s blocked!", username);
  31.                 break;
  32.             }
  33.             System.out.println("Incorrect password. Try again.");
  34.             enteredPassword = scanner.nextLine();
  35.         }
  36.  
  37.         //вярна парола -> enteredPassword == password
  38.         if (enteredPassword.equals(password)) {
  39.             System.out.printf("User %s logged in.", username);
  40.         }
  41.  
  42.  
  43.  
  44.     }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement