元音计数问题.. [英] vowel count problem..
本文介绍了元音计数问题..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string r;
r = Console.ReadLine();
int count = 0 ;
char [] vow = new char [] {' a',' e',' i',' o',' < span class =code-string> u'};
string res = new string (许愿);
for ( int x = 0 ; x < r.Length; x ++)
{
if (res.Contains(r [x]))
{
count ++;
}
Console.Write( 元音数:{0},count);
break ;
}
解决方案
如果你需要找到总元音数,不要在循环内使用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);
这篇关于元音计数问题..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文