HOW CAN I FORCE THE USER TO ENTER 26 DISTINCTIVE CAPITAL LETTERS IN C# pleaseee post solution
using System;
using System.Collections.Generic;
namespace CapitalLetters
{
class Program
{
static void ShowMessage(string message, ConsoleColor messageColor)
{
Console.ForegroundColor = messageColor;
Console.WriteLine(message);
Console.ResetColor();
Console.WriteLine(new string('-', 50));
}
static void Main(string[] args)
{
var allowedLetters = new List<char>();
for (char i = 'A'; i <= 'Z'; i++) //adding capital letters to list
allowedLetters.Add(i);
var acceptedLetters = new List<char>(); //here we will store accepted letters
int lettersQuantity = 26;
while (acceptedLetters.Count != 26)
{
Console.WriteLine($"You must enter {lettersQuantity} distinctive capital letters:");
if (!char.TryParse(Console.ReadLine(), out char enteredChar)) //check, that the input is only ONE char
{
ShowMessage("Error! You must enter just ONE capital letter!", ConsoleColor.Red);
continue;
}
if (!char.IsUpper(enteredChar) || !char.IsLetter(enteredChar)) //check, that input is a capital letter
{
ShowMessage("Error! You must enter a capital letter!", ConsoleColor.Red);
continue;
}
if (!allowedLetters.Contains(enteredChar)) //check, that input is allowed
{
ShowMessage($"Letter \"{enteredChar}\" already accepted.", ConsoleColor.Yellow);
continue;
}
ShowMessage($"Letter \"{enteredChar}\" accepted.", ConsoleColor.Green); //all checks done
lettersQuantity--;
allowedLetters.Remove(enteredChar);
acceptedLetters.Add(enteredChar);
}
Console.WriteLine($"Thank you! All {acceptedLetters.Count} capital letters accepted.");
}
}
}
Comments
Leave a comment