当条件为真时做 while 循环不循环

Do while loop not looping when condition is true

我的 do while 循环逻辑有什么问题?

我想阻止用户继续进入应用程序,除非他们提供正确的输入。

目前,它在崩溃并产生此异常之前只会接受一个错误的输入:

System.Exception: at disabler.Program.readInput(String Input) at disabler.Program.detectInput(String Input) at disabler.Program.Main(String[] args)

这是我的代码:

   private static string detectInput(string Input )
    {
        string result= Input;
        do
        {
            //pass input to readInput method
            if ((result == "exit") || (result == "query") || (result == "disable"))
            {
                readInput(result);
            }
            else 
            {
                Console.WriteLine("invalid input detected. Please try again.");
                result= Console.ReadLine();
                readInput(result);
            }
        }
        while (!((result == "exit") || (result == "query") || (result == "disable")));
        return result;
    }

这里是 readInput 方法的代码:

//Read user input
    private static string readInput(string Input)
    {
        int successfullyDisabled = 0;
        string input = string.Empty;
        switch (Input)
        {
            case "disable":
                            int disableCounter = 0;
                invalidAccount.ForEach(delegate(String samAccountName)
                {
                    disableCounter++;
                    Console.Write(disableCounter);
                    //disable inactive accounts
                    if (DisableADUser(samAccountName))
                    {
                        successfullyDisabled++;
                    }
                });

                invalidAccount.Clear();                    
                validAccount.Clear();
                Console.WriteLine("Press [ENTER] to continue or [ESC] to exit.\n");
                break;
            case "query":                   
                            Console.WriteLine("No.\t  | Dep | User Account\t       | Status");                               
                            int countRow = 0;
                validAccount.ForEach(delegate(String samAccountName)
                {
                    countRow++;
                    Console.Write(countRow);
                    //find status of active accounts
                    findUserStatus(samAccountName);
                });
                validAccount.Clear();
                invalidAccount.Clear();
                Console.WriteLine("Press [ENTER] to continue or [ESC] to exit.\n");
                break;
            case "exit":
                //leave console
                Environment.Exit(2);
                invalidAccount.Clear();
                validAccount.Clear();
                break;
            default:
                throw new Exception("Invalid command entered. Please enter command again.");
        }
        return input;
    }//end of ReadInput

您得到的异常是您在 readInput

的 switch 语句中抛出的异常

detectInput

的 else 中删除 readInput(result);

根据评论更新:

private static string detectInput(string Input )
{
    string result= Input;
    while (!((result == "exit") || (result == "query") || (result == "disable")));
    {            
            Console.WriteLine("invalid input detected. Please try again.");
            result= Console.ReadLine();
        }
    }

    result = readInput(result);

    return result;
}