蜘蛛女之吻 txt:C#问题!!!!!!!!!

来源:百度文库 编辑:高考问答 时间:2024/04/30 02:49:22
private bool check(string txt)
{
foreach(char chars in txt)
{
if(chars==' ')
{
return false;
}
else
{
return true;
}
}
}

提示以下错误:
c:\inetpub\wwwroot\newsWeb\Addnews.aspx.cs(82): “newsWeb.Addnews.check(string)” : 并非所有的代码路径都返回值

不知道你需要 Check 为你做什么?如果你希望它检查一个字符串是否为 null 或者 "",那么你可以有两个方法:

1) .NET 1.x:

private bool Check(string text)
{
return text == null || text.Length == 0;
}

2) .NET 2.0

直接使用 String.IsNullOrEmpty 方法。

另外,Check 方法体的逻辑有点问题,这块代码代表着当 txt 的第一个字符检查完毕就会马上返回而没有对第二个字符以及后面的字符(如果有的话)进行检查,因为 return 是直接导致方法返回的,如果逻辑上你希望跳到下一个循环,你应该使用 continue 关键字。

Hope that helps!

private bool check(string txt)
{
//这里
if txt.IsEmpty // 判断txt是否为空,是这样判断吗?
{
return false;
}
foreach(char chars in txt)
{
if(chars==' ')
{
return false;
}
else
{
return true;
}
}
}