-1

1.问题...这是我的密码验证。CONsoleApp 但我有错误...如果第一个空格是空程序工作(...但是如果第一个字符是白色,我想返回 false

2.Many Condition using I want some using condition..我想练习好方法

        {
            bool symb = false;
            bool letdig = false;
            char currentchar;
            char currentchar2;
            if (!(pass.Length >= 8 && pass.Length <= 25))
            {
                return true;
            }
            string symbols = "!@#$%^&*()_-+=[{]};:<>|./?.";
            char[] simbolchar = symbols.ToCharArray();


            for (int j = 0; j < pass.Length; j++)
            {
                currentchar = pass[j];
                foreach (var simb in simbolchar)
                {
                    if (simb == currentchar)
                    {
                        symb = true;
                    }
                }
                if (symb)
                {
                    for (int i = 0; i < pass.Length; i++)
                    {
                        currentchar2 = pass[i];
                        if (char.IsUpper(currentchar2) && (char.IsLetterOrDigit(currentchar2)))
                        {
                            letdig = true;
                        }
                    }
                }
                if (letdig)
                {
                    Console.WriteLine("WELCOME");
                    return true;
                }
            }
            return letdig;

        }
4

1 回答 1

0

解决第一个错误可以通过添加一个条件来检查字符串是否为空或为空,然后再进行任何检查

        public static bool passwordCheck(string pass)
        {
        bool symb = false;
        bool letdig = false;
        char currentchar;
        char currentchar2;
        //Solve the first bug
        if (string.IsNullOrEmpty(pass))
        {
            return false;
        }
        else
        {
            if (!(pass.Length >= 8 && pass.Length <= 25))
            {
                return true;
            }
            string symbols = "!@#$%^&*()_-+=[{]};:<>|./?.";
            char[] simbolchar = symbols.ToCharArray();


            for (int j = 0; j < pass.Length; j++)
            {
                currentchar = pass[j];
                foreach (var simb in simbolchar)
                {
                    if (simb == currentchar)
                    {
                        symb = true;
                    }
                }
                if (symb)
                {
                    for (int i = 0; i < pass.Length; i++)
                    {
                        currentchar2 = pass[i];
                        if (char.IsUpper(currentchar2) && (char.IsLetterOrDigit(currentchar2)))
                        {
                            letdig = true;
                        }
                    }
                }
                if (letdig)
                {
                    Console.WriteLine("WELCOME");
                    return true;
                }
            }
            return letdig;
        }
    }

至于代码优化,您可以阅读针对您的案例的清晰代码

于 2022-02-06T10:58:40.447 回答