调用方法时不会执行 while 循环

While loop does not get executed when method is called

while 循环

public static bool Authentication()
{
    bool isAuthenticated = false;
    int count = 4;
    while (isAuthenticated = false && count > 0)
    {
        Console.WriteLine("Please insert your 4 digit PIN");
        string pin = Console.ReadLine();
        if (pin == "1704")
        {
            isAuthenticated = true;
            return isAuthenticated;
        }
        else
        {
            count--;
            Console.WriteLine($"Wrong PIN: {count} Input remained ");
        }

    }

    return isAuthenticated;
}

我一直没能理解,当调试器进入while循环时,它并没有执行。可能是while的条件不符合逻辑。

我相信您在循环条件中使用 = 作为赋值运算符。您可以尝试 !isAuthenticated 而不是 isAuthenticated == false 看看是否有效

while (!isAuthenticate && count > 0)

我相信您将 false 分配给 isAuthenticate,因此条件永远不会为真。