Exercise
Password as string
Objetive
Write a C# program to ask the user for their username and password (both should be strings) and repeat it as many times as necessary until the entered name is "username" and the password is "password".
Example Code
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
}
}