通过按两次输入而不输入数据退出控制台应用程序

时间:2013-01-29 01:35:34

标签: c# asp.net

我想知道如何通过按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);

1 个答案:

答案 0 :(得分:0)

只需测试空输入。无需等待双&lt; enter&gt;:

while (true) {
    Console.Write("Enter Morse Code: \n");
    userInput = Console.ReadLine();
    if (userInput = "") {
        break;
    }
    // Process input here ....
}