Dianov

Basic Syntax, Conditional Statements and Loops - Exercise (05. Login)

Mar 26th, 2021
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Login
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string username = Console.ReadLine();          
  14.             string reversedUsername = string.Empty;
  15.  
  16.             for (int i = username.Length - 1; i >= 0; i--) // обръща username за сравнение на паролата.
  17.             {
  18.                 reversedUsername += username[i];
  19.             }
  20.  
  21.             int counter = 0; // брой опити за login в системата.
  22.  
  23.             while (true)
  24.             {
  25.                 string password = Console.ReadLine();
  26.                 counter++;
  27.                 if (password == reversedUsername)
  28.                 {
  29.                     Console.WriteLine($"User {username} logged in.");
  30.                     Environment.Exit(0);
  31.                 }
  32.                 else if (password != reversedUsername && counter == 4) // ако паролата на четвъртия опит е неправилна - блокира user-а.
  33.                 {
  34.                     Console.WriteLine($"User {username} blocked!");
  35.                     Environment.Exit(0);
  36.                 }
  37.                 else if (password != reversedUsername)
  38.                 {                  
  39.                     Console.WriteLine($"Incorrect password. Try again.");
  40.                 }              
  41.             }
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment