Ejercicio
Contraseña V2
Objetivo
Escriba un programa en C# para solicitar al usuario su nombre de usuario y su contraseña (ambos deben ser números enteros), hasta que el inicio de sesión ingresado sea "12" y la contraseña sea "1234". El usuario tendrá un máximo de 3 intentos.
Código de Ejemplo
using System; // Importing the System namespace to use Console functionalities
class Program
{
// Main method where the program execution begins
static void Main()
{
int login; // Declaring a variable to store the login
int password; // Declaring a variable to store the password
int attempts = 0; // Variable to count the number of attempts
// Repeatedly prompt the user for login and password until they enter the correct ones or exceed 3 attempts
while (attempts < 3) // Loop will repeat until attempts are less than 3
{
// Asking the user to enter their login (login should be an integer)
Console.Write("Enter your login: ");
login = int.Parse(Console.ReadLine()); // Reading the login entered by the user
// Asking the user to enter their password (password should be an integer)
Console.Write("Enter your password: ");
password = int.Parse(Console.ReadLine()); // Reading the password entered by the user
// Check if the entered login and password are correct
if (login == 12 && password == 1234)
{
// Display a success message and exit the loop
Console.WriteLine("Login successful!"); // Informing the user that the login was successful
return; // Exiting the program once the correct login and password are entered
}
else
{
// Incrementing the attempt counter if the login or password is incorrect
attempts++; // Increase the number of attempts made
Console.WriteLine($"Invalid login or password. You have {3 - attempts} attempts left."); // Informing the user about remaining attempts
}
}
// If the user exceeds the maximum number of attempts, display a failure message
Console.WriteLine("You have exceeded the maximum number of attempts. Access denied."); // Informing the user that they've failed
}
}