是否有代码来检查角色是元音还是辅音?像char = IsVowel这样的东西?还是需要硬编码?
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’:
case ‘A’:
case ‘E’:
case ‘I’:
case ‘O’:
case ‘U’:
Run Code Online (Sandbox Code Playgroud)
p.s*_*w.g 30
你可以这样做:
char c = ...
bool isVowel = "aeiouAEIOU".IndexOf(c) >= 0;
Run Code Online (Sandbox Code Playgroud)
或这个:
char c = ...
bool isVowel = "aeiou".IndexOf(c.ToString(), StringComparison.InvariantCultureIgnoreCase) >= 0;
Run Code Online (Sandbox Code Playgroud)
一旦你为诸如此类的东西添加国际支持,éèe??ëê?e?æø?这个字符串将变长,但基本的解决方案是相同的.
这是一个有效的功能:
public static class CharacterExtentions
{
public static bool IsVowel(this char c)
{
long x = (long)(char.ToUpper(c)) - 64;
if (x*x*x*x*x - 51*x*x*x*x + 914*x*x*x - 6894*x*x + 20205*x - 14175 == 0) return true;
else return false;
}
}
Run Code Online (Sandbox Code Playgroud)
使用它像:
char c = 'a';
if (c.IsVowel()) { // it's a Vowel!!! }
Run Code Online (Sandbox Code Playgroud)
(是的,它确实有效,但很明显,这是一个笑话答案.不要贬低我.或其他什么.)
不需要.您需要首先定义您认为的元音和辅音.例如,在英语中,"y"可以是辅音(如"是")或元音(如"by").像"é"和"ü"这样的字母可能是使用它们的所有语言中的元音,但似乎你根本不考虑它们.首先,您应该定义为什么要将字母分类为辅音和元音.