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个字符和小写,我需要它重复该过程,直到用验证规则输入一个单词。
我该怎么做?
答案 0 :(得分:2)
您的方法签名不正确,因为验证应返回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();