计算字符串中的元音和辅音


int total = 0;
int wordCount = 0, index = 0;
var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
var consonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x' };
for (int i = 0; i < sentence.Length; i++)
if (vowels.Contains(sentence[i]))
{
total++;
}
else if (consonants.Contains(sentence[i]))
{
total++;
}
}
Console.WriteLine("Your total number of vowels is: {0}", total);
Console.WriteLine("Number of consonants: {0}", total);
Console.ReadLine();
`

这是我的代码。当我运行代码时,它准确地告诉我有多少元音,但它没有告诉我辅音的数量。它只是复制了元音的数量。

int totalVowels = 0;
int totalConsonants = 0;
int wordCount = 0, index = 0;
var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
var consonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x' };
for (int i = 0; i < sentence.Length; i++)
{
if (vowels.Contains(sentence[i]))
{
totalVowels++;
}
else if (consonants.Contains(sentence[i]))
{
totalConsonants++;
}
}
Console.WriteLine("Your total number of vowels is: {0}", totalVowels);
Console.WriteLine("Number of consonants: {0}", totalConsonants);
Console.ReadLine();

在这里,您需要考虑一些事情来实现您的目标。输入字符串可能包含也可能不包含其他字符(特殊字符或数字),因此您应该检查输入字符串中的每个字符是否存在于vowelsconsonants中,还有一件事,您必须为vowelsconsonants保留单独的计数器,例如名为vowelsCountconsonantsCount。这意味着如果字符存在于vowels集合中,则应增加vowelsCount,如果该字符存在于consonants中,则应增加consonantsCount

此外,如果需要,您可以保留另一个变量来计算非字母字符。现在看一下以下代码:

int vowelsCount = 0, consonantsCount = 0, otherCharacterCount = 0;
string inputSenctnse = Console.ReadLine().ToLower();
var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
var consonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x' };
foreach (char letter in inputSenctnse)
{
if (vowels.Contains(letter))
vowelsCount++;
else if (consonants.Contains(letter))
consonantsCount++;
else
otherCharacterCount++;
}
Console.WriteLine("Your total number of vowels is: {0}", vowelsCount);
Console.WriteLine("Number of consonants: {0}", consonantsCount);
Console.WriteLine("Other characters : {0}", otherCharacterCount);
Console.ReadLine();

您需要使用两个变量来将两个数字都保留为 Vinh Vu。

我只是在这里发布另一个解决方案,但您仍然需要两个变量来保留它们:

int total = 0;
int wordCount = 0, index = 0;
var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
var consonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x' };
var sentence = "my sentence";
var vowelCount = sentence.ToCharArray().Where(x => vowels.Contains(x)).Count();
var consonantCount = sentence.ToCharArray().Where(x => consonants.Contains(x)).Count();

启动另一个变量名。在这里,我添加了变量 total2。

int total = 0; 
int total2 = 0;
int wordCount = 0, index = 0;
var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
var consonants = new HashSet<char> { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'x' };
for (int i = 0; i < sentence.Length; i++)
if (vowels.Contains(sentence[i]))
{
total++;
}
else if (consonants.Contains(sentence[i]))
{
total2++;
}
}
Console.WriteLine("Your total number of vowels is: {0}", total);
Console.WriteLine("Number of consonants: {0}", total2);
Console.ReadLine();

相关内容

  • 没有找到相关文章

最新更新