且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

元音计数问题..

更新时间:2023-02-26 08:09:34

如果你需要找到总元音数,不要在循环内使用break,它会退出在第一个字符之后



  for  int  x =  0 ; x <  r.Length; x ++)
{
if (vow.Contains(r [x]))
{
count ++;
}
}
Console.Write( 元音数:{0} ,count);


如果你使用Linq,这很容易:
  //   required  
使用 Linq;

string r = Console.ReadLine();

int count = r.Count(ch = > aeiou .Contains(ch));

Console.WriteLine( 元音数:{0},count );


string r;
            r = Console.ReadLine();
            int count = 0;
            char[] vow = new char[] { 'a', 'e', 'i', 'o', 'u' };
            string res = new string(vow);
            for (int x = 0; x < r.Length; x++)
            {

                if (res.Contains(r[x]))
                {
                    count++;

                }
                Console.Write("vowel count:{0}", count);
                break;
            }

if you need to find total vowel count, don't use break inside the loop, it will exit after first character

for (int x = 0; x < r.Length; x++)
{
    if (vow.Contains(r[x]))
    {
        count++;
    }
}
Console.Write("vowel count:{0}", count);


If you use Linq, this gets very easy:
// required
using Linq;

string r = Console.ReadLine();

int count = r.Count(ch => "aeiou".Contains(ch));

Console.WriteLine("vowel count:{0}", count);