public string ValidateWord() // A procedure that validates words
{
string strRet = "";
Console.WriteLine("Please enter a word 3 - 15 chars all lower case...");
strRet = Console.ReadLine();
//no validation at the minute....
return strRet;
}
我正在尝试制作一个可以验证单词的过程,应为3-15个字符,所有小写。如果不是3-15个字符,小写,我需要它重复该过程,直到输入验证规则。
。我该怎么做?
您的方法签名不正确,因为验证应返回 bool
:
public static bool IsValidWord(string word) // A procedure that validates words
{
return word.All(char.IsLower) && word.Length >= 3 && word.Length <= 15;
}
请注意,该方法使用LINQ
(Enumerable.All
),因此您需要添加using System.Linq;
。
现在您可以调用此方法,直到返回true
(省略了用户的提示)。
string word = Console.ReadLine();
while(!IsValidWord(word))
word = Console.ReadLine();