-1

我想知道如何通过按Enter两次退出控制台中的应用程序

string userInput; // to display back the exact user input if format for the code has been incorrectly entered
        string morseInput;
        do
        {
            Console.WriteLine("Enter Morse Code: \n");
            userInput = Console.ReadLine();
            morseInput = userInput.Replace(" ","");
            bool isValid = Regex.IsMatch(morseInput, @"^[-.]+$"); // only allow dots and dashes
            if (isValid)
            {
                Console.WriteLine("\nAll permutations:");
                var charString = new List<string>(Permutate(morseInput));
                foreach (string character in charString)
                    Write(character);
            }
            else // Error for improper input format
            {
                if (morseInput.Length == 0)
                    Console.WriteLine("Morse can not be zero-length.");
                else
                {
                    Console.WriteLine("\nFormat of morse must be only dots and dashes");
                    Console.WriteLine("Parameter name: " + userInput + "\n");
                }
            }
            Console.WriteLine("\n");
        }
        while (true);
4

1 回答 1

0

只需测试一个空输入。无需等待双 <enter>:

while (true) {
    Console.Write("Enter Morse Code: \n");
    userInput = Console.ReadLine();
    if (userInput = "") {
        break;
    }
    // Process input here ....
}
于 2013-01-29T13:42:17.870 回答