C#计算字符串中大写和小写字母的数量

R. *_*obs 0 c# string foreach for-loop

嗨我被要求做一个计算字符串中元音,辅音,大写和小写字母数量的任务.

我在编写一个字符串中的大写和小写字母时遇到了麻烦.我可以成功计算元音和常数的数量,但大写和小写字母似乎很痛苦.

这是代码:

    public void Calculate()
    {
        foreach(string sentence in sentenceList)
        {
            sentences++;

            for (int i = 0; i < sentence.Length; i++)
            {
                if (vowelsArray.Contains(sentence[i]))
                {
                    vowels++;
                }
                else if (consonantsArray.Contains(sentence[i]))
                {
                    consonants++;
                }
                else if (char.IsUpper(sentence[i]))
                {
                    upperCaseLetters++;
                }
                else if (char.IsLower(sentence[i]))
                {
                    lowerCaseLetters++;
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

大写和小写字母的值为0.(不应该)

有什么建议?谢谢!

Dmi*_*try 5

else以前不需要

else if (char.IsUpper(sentence[i]))
Run Code Online (Sandbox Code Playgroud)

因为您有两组独立的条件:

  1. 元音辅音
  2. 大写小写