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

C# 计算字符串中的元音和辅音,c#,string,visual-studio,cons,C#,String,Visual Studio,Cons,这是我的密码。当我运行代码时,它会准确地告诉我有多少个元音,但不会告诉我辅音的数量。它只是复制元音的数量。 这里,你需要考虑一些事情来实现你的目标。输入字符串可能包含也可能不包含其他字符—特殊字符或数字,因此您应该检查输入字符串中的每个字符是否存在于元音或辅音中,还有一件事,您必须为元音和辅音保留单独的计数器,例如名为元音计数、辅音计数。这意味着如果字符出现在元音集合中,那么元音计数应该增加,如果字符出现在辅音中,那么辅音计数应该增加 int total = 0;

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

这里,你需要考虑一些事情来实现你的目标。输入字符串可能包含也可能不包含其他字符—特殊字符或数字,因此您应该检查输入字符串中的每个字符是否存在于元音或辅音中,还有一件事,您必须为元音和辅音保留单独的计数器,例如名为元音计数、辅音计数。这意味着如果字符出现在元音集合中,那么元音计数应该增加,如果字符出现在辅音中,那么辅音计数应该增加

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

您需要使用两个变量来保持这两个数字为Vinh Vu

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

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

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

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

你是否意识到这是你第二次问。。它有完全相同的代码来打印相同的总变量。。?首先,计算不同变量中的辅音和元音。那么元音和辅音的总数是多少?这是怎么回事?@John3136,看起来op并没有阅读或试图理解他自己的代码。。当我运行代码时,它会准确地告诉我有多少个元音,但不会告诉我辅音的数量。它只是复制了元音的数量。你需要在单独的变量中对它们进行计数,这里你使用的是total来计算两者。谢谢你们,我很感激。它现在正在工作:]
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();