简体   繁体   中英

How do I make my program accept nothing but uppercase letters in C#?

This is a C# question. I want to make my program accept nothing but uppercase letters. I have managed to make it reject lowercase letters but I don't know how to make it reject numbers and other characters. Thank you for your help!

#region Question3
    /* Write an application named EnterUppercaseLetters that asks the user
     * to type an uppercase letter from the keyboard. If the character entered 
     * is an uppercase letter, display OK; if it isn't an uppercase letter, 
     * display an error message. The program continues until the user types an exclamation point.
     */
static void  EnterUppercaseLetters()
    {
        //char letter;
        bool toContinue = true;
        do
        {
            Console.Write("Enter an uppercase letter: ");
            //string input = Console.ReadLine();
            char letter = Convert.ToChar(Console.ReadLine());
            //double number = Convert.ToDouble(input);
            //int number = Convert.ToInt32(letter);
            if (letter == '!')
            {
                toContinue = false;
            }
            else
            {
                if (letter == Char.ToUpper(letter))
                {
                    Console.WriteLine($"{letter} OK");
                }
                else
                {
                    Console.WriteLine("ERROR!");
                    continue;
                }
            }
        } while (toContinue);
        Console.WriteLine();
    }

#endregion

@Moe - what you've got is good... but maybe you'd prefer not entering the whole line. You can try something like this instead:

static void Main(string[] args)
{
    char ch;
        
    //input character 
    Console.WriteLine("Enter an UPPER CASE character, or '!' to exit: ");
    while ((ch = Console.ReadKey().KeyChar() != '!')
    {
       if (IsUpper(ch))
            Console.WriteLine("Input character is {0}: OK", ch);
       else 
            Console.WriteLine("Input character is {0}: ERROR!", ch);
    }
}

This will respond immediately when you enter ANY keystroke. It will exit immediately when you type ".". And it will print "OK" (for an upper case character) or "ERROR!" otherwise.

Relevant documentation (for our peripatetic friend Jeppe Stig Nielsen):

I think you can use regex for that.

Regex.IsMatch(input, @"^[A-Z]+$");

This will check if your string has any character that is not a capital letter.

One possibility is:

char.GetUnicodeCategory(letter) != UnicodeCategory.UppercaseLetter

Note that Convert.ToChar will throw an unhandled exception if the user types an empty string or a string with length exceeding one. You may consider checking the string length, or using Console.ReadKey instead of Console.ReadLine .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM