谁能帮我看一段简单的用c#统计字符串中某个字符出现次数的程序?
结果不正确,是因为中间item赋值有问题吗?提前感谢!namespaceConsoleApplication1{classProgram{staticvoidMain(s...
结果不正确,是因为中间item赋值有问题吗? 提前感谢!
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int iSumofC = 0;
string sTextline = "The Austin, Texas-based music and media festival, South by Southwest opens March 8th. The ten-day long festival brings hundreds of artists, entertainers and media interests to town. Christopher Cruise plays music from some of this year's guests.";
foreach (var item in sTextline);
{
int item = 0;
if (item == 'c') ;
{ iSumofC = iSumofC + 1; }
}
Console.WriteLine("c freq={0}",iSumofC);
Console.ReadKey();
}
}
} 展开
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int iSumofC = 0;
string sTextline = "The Austin, Texas-based music and media festival, South by Southwest opens March 8th. The ten-day long festival brings hundreds of artists, entertainers and media interests to town. Christopher Cruise plays music from some of this year's guests.";
foreach (var item in sTextline);
{
int item = 0;
if (item == 'c') ;
{ iSumofC = iSumofC + 1; }
}
Console.WriteLine("c freq={0}",iSumofC);
Console.ReadKey();
}
}
} 展开
2个回答
展开全部
int item = 0;
if (item == 'c') ;
{ iSumofC = iSumofC + 1; }
你这里int型和字符型比较是想干嘛?你干嘛不直接用foreach循环里面的item直接判断就可以了。而且现在统计这个出现的次数可以更加快捷的方法。
if (item == 'c') ;
{ iSumofC = iSumofC + 1; }
你这里int型和字符型比较是想干嘛?你干嘛不直接用foreach循环里面的item直接判断就可以了。而且现在统计这个出现的次数可以更加快捷的方法。
追问
我刚开始学c#, 这是老师上课讲的实例,但我没记住。int item 这句是我自己乱加的。
因为如果没有这句,就提示:当前上下文中不存在名称“item”。弄不明白啊。
追答
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int iSumofC = 0;
string sTextline = "The Austin, Texas-based music and media festival, South by Southwest opens March 8th. The ten-day long festival brings hundreds of artists, entertainers and media interests to town. Christopher Cruise plays music from some of this year's guests.";
foreach (var item in sTextline)
{
if (item == 'c')
{
iSumofC = iSumofC + 1;
}
}
Console.WriteLine("c freq={0}", iSumofC);
Console.ReadKey();
}
}
}
我复制出来改了下,现在编译通过了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询