简体   繁体   English

如何让我的程序只接受 C# 中的大写字母?

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

This is a C# question.这是一个 C# 问题。 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 #endregion

@Moe - what you've got is good... but maybe you'd prefer not entering the whole line. @Moe - 你所拥有的很好......但也许你不想输入整条线。 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!"它将打印“OK”(对于大写字符)或“ERROR!” otherwise.否则。

Relevant documentation (for our peripatetic friend Jeppe Stig Nielsen):相关文件(给我们的四处游荡的朋友 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.请注意,如果用户键入空字符串或长度超过 1 的字符串,则Convert.ToChar将引发未处理的异常。 You may consider checking the string length, or using Console.ReadKey instead of Console.ReadLine .您可以考虑检查字符串长度,或使用Console.ReadKey而不是Console.ReadLine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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