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

时间:2016-12-19 04:11:39

标签: c# string visual-studio cons

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

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

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

在这里,你需要考虑一些事情来实现你的目标。输入字符串可能包含也可能不包含其他字符(特殊字符或数字),因此您应该检查输入字符串中的每个字符是否存在于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();

答案 2 :(得分:0)

您需要使用两个变量将两个数字保持为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();

答案 3 :(得分:0)

启动另一个变量名称。这里我添加了变量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();
相关问题