验证用户只输入一个单词的答案并且第一个字母大写
Validating user only enters a one word answer and that the first letter is capitalized
我目前正在自学 C# 并且对它有相当不错的掌握,但是我不知道如何验证用户只输入了一个单词答案并且它是大写的,否则我想给他们又一次尝试的机会。
这是我目前拥有的:
static void Main(string[] args)
{
//assigned variable
string userInput;
//intializing empty string
string answerInput = string.Empty;
//Creating loop
while ((answerInput == string.Empty) || (answerInput == "-1"))
{
//This is asking the question to the user
Console.WriteLine("Enter your favorite animal: ");
//this is storing users input
userInput = Console.ReadLine();
//using function to validate response
answerInput = letterFunc(userInput);
}
}
//Creating function to only allow letters and making sure it's not left blank.
private static string letterFunc (string validate)
{
//intializing empty string
string returnString = string.Empty;
//validating it is not left empty
if(validate.Length > 0)
{
//iterating through the passed string
foreach(char c in validate)
{
//using the asciitable to validate they only use A-Z, a-z, and space
if ((((Convert.ToInt32(c)) > 64) && ((Convert.ToInt32(c)) < 91)) || (((Convert.ToInt32(c)) > 96) && ((Convert.ToInt32(c)) < 123)) || (Convert.ToInt32(c) == 32))
{
//appensing sanitized character to return string
returnString += c;
}
else
{
//If they try to enter a number this will warn them
Console.WriteLine("Invalid input. Use letters only.");
}
}
}
else
{
//If user enters a blank input, this will warn them
Console.WriteLine("You cannot enter a blank response.");
}
//returning string
return returnString;
}
我想知道是否可以在我创建的函数中执行此操作以验证它们仅使用字母并且它不为空并提供详细说明。谢谢
正则表达式并不难。问题是有时你想要实现更复杂的东西,但那不是你的情况:
private static string letterFunc (string validate)
{
return new Regex("^[A-Z][a-z]*$").IsMatch(validate) ?
validate :
string.Empty;
}
解释表达式:
^
- 锚点:仅当文本 以 表达式
开头时才匹配
[A-Z]
- 恰好一个字符,从 A 到 Z
[a-z]*
- 零个或多个字符,从 a 到 z
$
- 锚点:仅当文本 以表达式
结束 时才匹配
通过使用两个锚点,我们希望全文匹配表达式,而不是部分表达式。
如果你想在首字母后允许大写(如CaT
或DoG
),你可以将其更改为:
^[A-Z][a-zA-Z]*$
正则表达式是执行此操作的标准方法,但看看您的代码,我认为您还没有准备好使用它们。顺便说一句,这不是侮辱——每个人都曾是初学者!
每当您遇到这样的问题时,首先要确保您的所有要求都定义明确且具体:
一句话回答:在您的代码中,您将其定义为"an answer that contains only letters and spaces"。这可能并不理想,因为它会阻止人们输入像 dik-dik 这样的答案作为他们最喜欢的动物。但让我们暂时坚持下去。
大写答案:让我们将其定义为"an answer where the first character is a capital letter"。
因此,将这两个要求放在一起,我们试图验证答案是否以大写字母开头,并且仅包含字母和 spaces。
在编码时,查看您使用的语言和框架,看看是否有方便的方法可以帮助您。 .NET 有很多这样的东西。我们知道我们必须检查 String
的单个字符,而一个字符串由 Char
组成,所以让我们 google "c# char type"。查看 MSDN page for System.Char,我们可以看到一些可能对我们有帮助的方法:
Char.IsWhiteSpace
- 测试字符是否为 'whitespace'(space、制表符、换行符)
Char.IsLetter
- 测试字符是否为字母。
Char.IsUpper
- 测试字符是否为大写字母。
那么让我们再看看我们的需求,一次一个地实现它们:"starts with a capital letter and contains only letters and spaces".
让我们称我们的用户输入字符串为 answer
。我们可以像这样检查第一个字母是否为大写(请注意,我们还要确保它有第一个字母):
bool isCapitalized = answer.Length > 0 && Char.IsUpper( answer[0] );
我们可以检查它是否只包含字母和 space,如下所示:
bool containsOnlyLettersAndSpaces = true;
foreach( char c in answer )
{
if( !Char.IsLetter( c ) && !Char.IsWhiteSpace( c ) )
{
containsOnlyLettersAndSpaces = false;
break;
}
}
containsOnlyLettersAndSpaces
开始为真。然后我们查看字符串中的每个字符。如果我们发现一个字符不是字母且不是白色 space,我们将 containsOnlyLettersAndSpaces
设置为 false。此外,如果我们发现无效字符,则停止检查。我们现在知道答案无效,没有理由检查它的其余部分!
现在我们可以 return 我们的答案是两个验证的组合:
return isCapitalized && containsOnlyLettersAndSpaces;
这是整个方法:
private bool IsValidAnimal( string answer )
{
bool isCapitalized = answer.Length > 0 && Char.IsUpper( answer[0] );
bool containsOnlyLettersAndSpaces = true;
foreach( char c in answer )
{
if( !Char.IsLetter( c ) && !Char.IsWhiteSpace( c ) )
{
containsOnlyLettersAndSpaces = false;
break;
}
}
return isCapitalized && containsOnlyLettersAndSpaces;
}
祝你学习 C# 好运,我希望这对你思考如何编码有所帮助!
我明白了。感谢大家的帮助。
string answer;
while (true)
{
Console.WriteLine("Enter your favorite animal:");
answer = Console.ReadLine();
if (new Regex("^[A-Z][a-z]*$").IsMatch(answer))
{
Console.WriteLine("You like {0}s. Cool!", answer);
Console.ReadKey();
break;
}
else
{
Console.WriteLine("'{0}' is not a valid answer.", answer);
Console.WriteLine();
Console.WriteLine("Make sure:");
Console.WriteLine("You are entering a one word answer.");
Console.WriteLine("You are only using letters.");
Console.WriteLine("You are capitalizing the first letter of the word.");
Console.WriteLine();
Console.WriteLine("Try again:");
}
}
我目前正在自学 C# 并且对它有相当不错的掌握,但是我不知道如何验证用户只输入了一个单词答案并且它是大写的,否则我想给他们又一次尝试的机会。
这是我目前拥有的:
static void Main(string[] args)
{
//assigned variable
string userInput;
//intializing empty string
string answerInput = string.Empty;
//Creating loop
while ((answerInput == string.Empty) || (answerInput == "-1"))
{
//This is asking the question to the user
Console.WriteLine("Enter your favorite animal: ");
//this is storing users input
userInput = Console.ReadLine();
//using function to validate response
answerInput = letterFunc(userInput);
}
}
//Creating function to only allow letters and making sure it's not left blank.
private static string letterFunc (string validate)
{
//intializing empty string
string returnString = string.Empty;
//validating it is not left empty
if(validate.Length > 0)
{
//iterating through the passed string
foreach(char c in validate)
{
//using the asciitable to validate they only use A-Z, a-z, and space
if ((((Convert.ToInt32(c)) > 64) && ((Convert.ToInt32(c)) < 91)) || (((Convert.ToInt32(c)) > 96) && ((Convert.ToInt32(c)) < 123)) || (Convert.ToInt32(c) == 32))
{
//appensing sanitized character to return string
returnString += c;
}
else
{
//If they try to enter a number this will warn them
Console.WriteLine("Invalid input. Use letters only.");
}
}
}
else
{
//If user enters a blank input, this will warn them
Console.WriteLine("You cannot enter a blank response.");
}
//returning string
return returnString;
}
我想知道是否可以在我创建的函数中执行此操作以验证它们仅使用字母并且它不为空并提供详细说明。谢谢
正则表达式并不难。问题是有时你想要实现更复杂的东西,但那不是你的情况:
private static string letterFunc (string validate)
{
return new Regex("^[A-Z][a-z]*$").IsMatch(validate) ?
validate :
string.Empty;
}
解释表达式:
^
- 锚点:仅当文本 以 表达式
[A-Z]
- 恰好一个字符,从 A 到 Z
[a-z]*
- 零个或多个字符,从 a 到 z
$
- 锚点:仅当文本 以表达式
通过使用两个锚点,我们希望全文匹配表达式,而不是部分表达式。
如果你想在首字母后允许大写(如CaT
或DoG
),你可以将其更改为:
^[A-Z][a-zA-Z]*$
正则表达式是执行此操作的标准方法,但看看您的代码,我认为您还没有准备好使用它们。顺便说一句,这不是侮辱——每个人都曾是初学者!
每当您遇到这样的问题时,首先要确保您的所有要求都定义明确且具体:
一句话回答:在您的代码中,您将其定义为"an answer that contains only letters and spaces"。这可能并不理想,因为它会阻止人们输入像 dik-dik 这样的答案作为他们最喜欢的动物。但让我们暂时坚持下去。
大写答案:让我们将其定义为"an answer where the first character is a capital letter"。
因此,将这两个要求放在一起,我们试图验证答案是否以大写字母开头,并且仅包含字母和 spaces。
在编码时,查看您使用的语言和框架,看看是否有方便的方法可以帮助您。 .NET 有很多这样的东西。我们知道我们必须检查 String
的单个字符,而一个字符串由 Char
组成,所以让我们 google "c# char type"。查看 MSDN page for System.Char,我们可以看到一些可能对我们有帮助的方法:
Char.IsWhiteSpace
- 测试字符是否为 'whitespace'(space、制表符、换行符)Char.IsLetter
- 测试字符是否为字母。Char.IsUpper
- 测试字符是否为大写字母。
那么让我们再看看我们的需求,一次一个地实现它们:"starts with a capital letter and contains only letters and spaces".
让我们称我们的用户输入字符串为 answer
。我们可以像这样检查第一个字母是否为大写(请注意,我们还要确保它有第一个字母):
bool isCapitalized = answer.Length > 0 && Char.IsUpper( answer[0] );
我们可以检查它是否只包含字母和 space,如下所示:
bool containsOnlyLettersAndSpaces = true;
foreach( char c in answer )
{
if( !Char.IsLetter( c ) && !Char.IsWhiteSpace( c ) )
{
containsOnlyLettersAndSpaces = false;
break;
}
}
containsOnlyLettersAndSpaces
开始为真。然后我们查看字符串中的每个字符。如果我们发现一个字符不是字母且不是白色 space,我们将 containsOnlyLettersAndSpaces
设置为 false。此外,如果我们发现无效字符,则停止检查。我们现在知道答案无效,没有理由检查它的其余部分!
现在我们可以 return 我们的答案是两个验证的组合:
return isCapitalized && containsOnlyLettersAndSpaces;
这是整个方法:
private bool IsValidAnimal( string answer )
{
bool isCapitalized = answer.Length > 0 && Char.IsUpper( answer[0] );
bool containsOnlyLettersAndSpaces = true;
foreach( char c in answer )
{
if( !Char.IsLetter( c ) && !Char.IsWhiteSpace( c ) )
{
containsOnlyLettersAndSpaces = false;
break;
}
}
return isCapitalized && containsOnlyLettersAndSpaces;
}
祝你学习 C# 好运,我希望这对你思考如何编码有所帮助!
我明白了。感谢大家的帮助。
string answer;
while (true)
{
Console.WriteLine("Enter your favorite animal:");
answer = Console.ReadLine();
if (new Regex("^[A-Z][a-z]*$").IsMatch(answer))
{
Console.WriteLine("You like {0}s. Cool!", answer);
Console.ReadKey();
break;
}
else
{
Console.WriteLine("'{0}' is not a valid answer.", answer);
Console.WriteLine();
Console.WriteLine("Make sure:");
Console.WriteLine("You are entering a one word answer.");
Console.WriteLine("You are only using letters.");
Console.WriteLine("You are capitalizing the first letter of the word.");
Console.WriteLine();
Console.WriteLine("Try again:");
}
}