C#练习-输入一个字符串,判断其是否是C#的合法标识符。

C# 标识符命名规则

        string str = Console.ReadLine();
        bool isRight = true;
        if ((str[0] >= 'a' && str[0] <= 'z') || (str[0] >= 'A' && str[0] <= 'Z') || str[0] == '_' || str[0] == '@')
        {
        }
        else
        {
            isRight = false;
        }

        for (int i = 1; i < str.Length; i++)
        {
            if ((str[i]>='a'&& str[i] <= 'z')||(str[i]>='A'&& str[i] <= 'Z')||str[i]=='_' ||(str[i]>='0'&&str[i]<='9'))
            {
            }
            else
            {
                isRight = false;
                break;
            }
        }

        if (isRight==true)
        {
            Console.WriteLine(str+" 是合法字符串");
        }
        else
        {
            Console.WriteLine(str + " 不是合法字符串");
            
        }

你可能感兴趣的:(C#练习题)