在循环中读取密码确认和重试时的错误

本文关键字:重试 错误 确认 循环 读取 密码 | 更新日期: 2023-09-27 17:49:29

编辑:我面临的问题是,我输入一个不正确的密码来确认以前的密码,它的工作,因为它是第一次尝试。最大值是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,然后重试他们输入password, confirmPasspassword2password
  2. 在改变这个之后,流没有正常工作,所以我从while中删除了confirmPass != pass &&位。这允许您进入该循环中的elseif部分。
  3. 不过,我认为这有点错,因为一旦你击中elseif,它将无限循环。所以我告诉breakelseif里面杀死那个do/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);
}

现在这些bug变得更容易发现了。我在评论中使用<===突出显示了它们。我将把它留给你来找出问题所在,以及如何解决它。