读取密码并确认并循环重试时出现错误

Bug when reading password with confirmation and retries in a loop

编辑:我面临的问题是我输入了错误的密码来确认以前的密码,但由于这是第一次尝试,所以它起作用了。最多尝试 3 次。在第 2 次和第 3 次尝试期间,即使密码正确,它也会说密码无效。 我不想要那个

我知道我遗漏了一些非常微不足道的东西,但我无法找到它。这是代码:

class Program
{
    static void Main(string[] args)
    {
        string username = "";
        string pass = "";
        string confirmPass = "";
        Console.Write("Enter username: ");
        username = Console.ReadLine();
        Console.WriteLine();
        Console.Write("Enter your password: ");
        ConsoleKeyInfo key;

        do
        {
            key = Console.ReadKey(true);

            // Backspace Should Not Work
            if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
            {
                pass += key.KeyChar;
                Console.Write("*");
            }
            else if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
            {
            pass = pass.Substring(0, (pass.Length - 1));
            Console.Write("\b \b");
            }

        // Stops Receving Keys Once Enter is Pressed
        } while (key.Key != ConsoleKey.Enter);

        Console.WriteLine("");
        Console.WriteLine("Confirm your password: ");
        ConsoleKeyInfo confirmKey;

        int retryCount = 3;

        string finalPass = "";
        do{


            confirmKey = Console.ReadKey(true);

            // Backspace Should Not Work
            if (confirmKey.Key != ConsoleKey.Backspace && confirmKey.Key != ConsoleKey.Enter)
            {
                confirmPass += confirmKey.KeyChar;
                Console.Write("*");
            }
            else if (confirmKey.Key == ConsoleKey.Backspace && pass.Length > 0)
            {
                confirmPass = confirmPass.Substring(0, (confirmPass.Length - 1));
                Console.Write("\b \b");
            }
            //

            // Stops Receving Keys Once Enter is Pressed
        } while ((confirmKey.Key != ConsoleKey.Enter));

        //Console.WriteLine("");
        //string confirmPass = "";

        do
        {
            if (confirmPass != pass)
            {

                Console.WriteLine("Re-enter Password: (" + retryCount + " tries remaining)");

                do
                {


                   confirmKey = Console.ReadKey(true);

                    // Backspace Should Not Work
                    if (confirmKey.Key != ConsoleKey.Backspace && confirmKey.Key != ConsoleKey.Enter)
                    {
                        confirmPass += confirmKey.KeyChar;
                        Console.Write("*");
                    }
                    else if (confirmKey.Key == ConsoleKey.Backspace && pass.Length > 0)
                    {
                        confirmPass = confirmPass.Substring(0, (confirmPass.Length - 1));
                        Console.Write("\b \b");
                    }
                    //

                    // Stops Receving Keys Once Enter is Pressed
                } while ((confirmKey.Key != ConsoleKey.Enter));
                retryCount--;

            }

            else if (confirmPass == pass)
            {
                Console.WriteLine("Enter password to log in :");
                finalPass = Console.ReadLine();
                if (finalPass == pass)
                {
                    Console.WriteLine("Login Successful. Welcome " + username + "!");
                    Console.WriteLine();
                    Console.WriteLine("Test Successful. Press Enter to quit.");
                }
            }
            if (retryCount == 0)
            {
                Console.WriteLine("Exceeded number of tries!!");
                Console.ReadLine();
            }
        } while (confirmPass != pass && retryCount != 0);
    }
}

根据你的代码,这就是我想出的:

public static void Main(string[] args)
{
    string username = "";
    string pass = "";
    string confirmPass = "";
    Console.Write("Enter username: ");
    username = Console.ReadLine();
    Console.WriteLine();
    Console.Write("Enter your password: ");
    ConsoleKeyInfo key;

    do
    {
        key = Console.ReadKey(true);

        // Backspace Should Not Work
        if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
        {
            pass += key.KeyChar;
            Console.Write("*");
        }
        else if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
        {
            pass = pass.Substring(0, (pass.Length - 1));
            Console.Write("\b \b");
        }
        // Stops Receving Keys Once Enter is Pressed
    } while (key.Key != ConsoleKey.Enter);

    Console.WriteLine("");
    Console.WriteLine("Confirm your password: ");
    ConsoleKeyInfo confirmKey;

    int retryCount = 3;

    string finalPass = "";
    do
    {
        confirmKey = Console.ReadKey(true);
        // Backspace Should Not Work
        if (confirmKey.Key != ConsoleKey.Backspace && confirmKey.Key != ConsoleKey.Enter)
        {
            confirmPass += confirmKey.KeyChar;
            Console.Write("*");
        }
        else if (confirmKey.Key == ConsoleKey.Backspace && pass.Length > 0)
        {
            confirmPass = confirmPass.Substring(0, (confirmPass.Length - 1));
            Console.Write("\b \b");
        }
        // Stops Receving Keys Once Enter is Pressed
    } while ((confirmKey.Key != ConsoleKey.Enter));

    do
    {
        if (confirmPass != pass)
        {
            confirmPass = "";
            Console.WriteLine("Re-enter Password: (" + retryCount + " tries remaining)");

            do
            {
                confirmKey = Console.ReadKey(true);

                // Backspace Should Not Work
                if (confirmKey.Key != ConsoleKey.Backspace && confirmKey.Key != ConsoleKey.Enter)
                {
                    confirmPass += confirmKey.KeyChar;
                    Console.Write("*");
                }
                else if (confirmKey.Key == ConsoleKey.Backspace && pass.Length > 0)
                {
                    confirmPass = confirmPass.Substring(0, (confirmPass.Length - 1));
                    Console.Write("\b \b");
                }
                // Stops Receving Keys Once Enter is Pressed
            } while ((confirmKey.Key != ConsoleKey.Enter));
            retryCount--;
        }
        else if (confirmPass == pass)
        {
            Console.WriteLine("Enter password to log in :");
            finalPass = Console.ReadLine();
            if (finalPass == pass)
            {
                Console.WriteLine("Login Successful. Welcome " + username + "!");
                Console.WriteLine();
                Console.WriteLine("Test Successful. Press Enter to quit.");
                Console.ReadLine();
                break;
            }
        }
        if (retryCount == 0)
        {
            Console.WriteLine("Exceeded number of tries!!");
            Console.ReadLine();
        }
    } while (retryCount != 0);
}

基本上我做了以下改动:

  1. 当您进入最后一个 do 循环并输入第一个 if 语句时,我设置 confirmPass = "";。这就是您的代码最初没有执行该部分的原因,因为您将它们的 new confirmPass 附加到原始代码。因此,如果他们输入 password,然后输入 password2,然后重试他们输入 passwordconfirmPasspassword2password
  2. 更改此后,流程无法正常工作,因此我从 while 中删除了 confirmPass != pass && 位。这使您可以到达该循环中的 elseif 部分。
  3. 不过,我认为这有点不对,因为一旦你点击 elseif 它就会无限循环。所以我告诉它 breakelseif 里面 killsdo/while 循环。

如果此解决方案不正确,请说明您的要求以便我们更好地帮助您。

您的程序相当复杂,因此很难发现任何错误。你真的应该学习如何使用子例程(即函数),这样你就不必一遍又一遍地重复做同样事情的代码块(比如读取密码)。

如果我们要稍微重构您的代码,将那些重复的部分移出到名为 ReadPassword 的方法中,如下所示:

public static string ReadPassword()
{
    var pass = "";
    while (true)
    {
        var key = Console.ReadKey(true);
        if (key.Key == ConsoleKey.Enter)
            // Stop Receving Keys Once Enter is Pressed
            break;

        // Backspace Should Not Work
        if (key.Key != ConsoleKey.Backspace)
        {
            pass += key.KeyChar;
            Console.Write("*");
        }
        else if (pass.Length > 0)
        {
            pass = pass.Substring(0, (pass.Length - 1));
            Console.Write("\b \b");
        }
    }
    return pass;
}

并用它来替换所有那些用于读取密码的 do ... while 循环,无需更改代码中的任何其他内容(因此错误仍然存​​在):

public static void Main(params string[] args)
{
    string username = "";
    string pass = "";
    string confirmPass = "";
    Console.Write("Enter username: ");
    username = Console.ReadLine();
    Console.WriteLine();
    Console.Write("Enter your password: ");
    pass = ReadPassword(); // changed to function call

    Console.WriteLine("");
    Console.WriteLine("Confirm your password: ");
    int retryCount = 3;
    string finalPass = "";
    confirmPass = ReadPassword(); // changed to function call

    do
    {
        if (confirmPass != pass) // <==== this is performed before the "else"
        {
            Console.WriteLine("Re-enter Password: (" + retryCount + " tries remaining)");
            confirmPass = ReadPassword(); // changed to function call
            retryCount--;
        }
        else if (confirmPass == pass) // <==== "else": bug
        {
            // Bug: it will only get here, if the confirmed password 
            // was entered correctly the first time
            Console.WriteLine("Enter password to log in :");
            finalPass = Console.ReadLine();
            if (finalPass == pass)
            {
                Console.WriteLine("Login Successful. Welcome " + username + "!");
                Console.WriteLine();
                Console.WriteLine("Test Successful. Press Enter to quit.");
            }
            // else <==== now what?
        }
        if (retryCount == 0)
        {
            Console.WriteLine("Exceeded number of tries!!");
            Console.ReadLine();
        }
    } while (confirmPass != pass && retryCount != 0);
}

现在更容易发现错误。我在评论中使用 <=== 突出显示了它们。我会留给你找出问题所在,以及如何解决它。