我想知道如何通过按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);
答案 0 :(得分:0)
只需测试空输入。无需等待双&lt; enter&gt;:
while (true) {
Console.Write("Enter Morse Code: \n");
userInput = Console.ReadLine();
if (userInput = "") {
break;
}
// Process input here ....
}