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

Counting vowel and consonant in a string

            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。

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

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();