Ejercicio
Contraseña como cadena
Objetivo
Escriba un programa en C# para pedirle al usuario su nombre y su contraseña (ambos deben ser cadenas) y repítalo tantas veces como sea necesario, hasta que el nombre ingresado sea "usuario" y la contraseña sea "contraseña".
Código de Ejemplo
using System; // Import the System namespace to use basic classes like Console
class Program // Define the main class of the program
{
static void Main() // The entry point of the program
{
string username; // Declare a variable to store the username
string password; // Declare a variable to store the password
// Loop until the correct username and password are entered
do
{
// Ask the user for their username
Console.Write("Enter your username: ");
username = Console.ReadLine(); // Read the username input from the user
// Ask the user for their password
Console.Write("Enter your password: ");
password = Console.ReadLine(); // Read the password input from the user
// Check if the username and password are correct
if (username != "username" || password != "password") // If either is incorrect
{
Console.WriteLine("Invalid username or password, please try again."); // Display error message
}
} while (username != "username" || password != "password"); // Repeat until both are correct
// When correct username and password are entered
Console.WriteLine("Welcome! You have successfully logged in."); // Welcome message
}
}