帮我看看我的代码哪里出了错!有关递归算法的,在线等候!! 最后加一句return null;因为你的foreach语句里的内容不一定就被执行 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 估计是你这里有问题:StreamWriter srMyfile = new StreamWriter(fsMyfile,System.Text.Encoding.Default); //System.Text.Encoding.Default 保证中文显示正确 return GetPath(Gid,f.FullName);Gid是什么? return GetPath(Gid,f.FullName);//对每个子目录递归调用解释一下 最后加一个 return ;看看 对不起,上面的一句return GetPath(Gid,f.FullName);//对每个子目录递归调用改为 return GetPath(name,f.FullName);//对每个子目录递归调用请各位大虾再仔细看看为什么编译时报告: 编译时错误: “GetPath(string, string)” : 并非所有的代码路径都返回值 public static string GetPath(string name,string initpath){ 。。return "";} public static string GetPath(string name,string initpath){ //根据name,在初始路径initpath下搜索是否存在 //以naem命名的文件夹,若有,则返回其绝对路径,若无 //则返回空串,使用递归算法 //调用时传入所要搜索的文件夹名及所要搜索的初始路径 if(name=="") return""; if(initpath=="") return ""; DirectoryInfo dir=new DirectoryInfo(initpath); if(!dir.Exits) return ""; DirectoryInfo[] subdirs=dir.GetDirectories(initpath); if(subdirs.Length==0) return"";//不存在,返回空串 foreach(DirectoryInfo f in subdirs) { if(f.Name==name) return f.FullName;//返回所要的文件夹的绝对路径 else return GetPath(Gid,f.FullName);//对每个子目录递归调用 }return "没有找到";} string aaa;foreach(DirectoryInfo f in subdirs) { if(f.Name==name) aaa= f.FullName;//返回所要的文件夹的绝对路径 else aaa= GetPath(Gid,f.FullName);//对每个子目录递归调用 }return aaa; string aaa;...if(...) aaa="";foreach(DirectoryInfo f in subdirs) { if(f.Name==name) aaa= f.FullName;//返回所要的文件夹的绝对路径 else aaa= GetPath(Gid,f.FullName);//对每个子目录递归调用 }return aaa; 问题搞定,经过优化后得到一个比较不错的结果public static string GetPath(string name,string initpath){ /*根据name,在初始路径initpath下搜索是否存在 *以naem命名的文件夹,若有,则返回其绝对路径,若无 *则返回空串,使用递归算法 *调用时传入所要搜索的文件夹名及所要搜索的初始路径*/ if(name==""||initpath=="") return""; DirectoryInfo dir=new DirectoryInfo(initpath); if(!dir.Exits) return ""; DirectoryInfo[] subdirs=dir.GetDirectories();//原来的函数在这里有错 foreach(DirectoryInfo f in subdirs) { if(f.Name==name) return f.FullName;//返回所要的文件夹的绝对路径 else return GetPath(name,f.FullName);//对每个子目录递归调用 } return "";} 求一个千千静听的源代码(或其他播放器代码) word文档的第一页是否可以转化为1个图片 通常每个套接字地址(协议/网络地址/端口)只允许使用一次 數據庫操作,高手求救 存储过程参数传入问题?? 求助....一个有关票据打印机的问题.... 问个简单的问题,switch语法中case的数量最大是多少? 有兴趣讨论一下关于防止网页的源码抓取吗? 关于ENA13条码的校验码问题 C# Webbrower<Select>控件选值问题 为什么我在C#中生成的htm是乱码,在txt中却是正常汉字呢! 用Response.Write()打开页面怎么让打开的页面在屏幕是居中?
//System.Text.Encoding.Default 保证中文显示正确
return GetPath(Gid,f.FullName);//对每个子目录递归调用
解释一下
改为 return GetPath(name,f.FullName);//对每个子目录递归调用请各位大虾再仔细看看为什么编译时报告:
编译时错误:
“GetPath(string, string)” : 并非所有的代码路径都返回值
{
。。
return "";
}
{ //根据name,在初始路径initpath下搜索是否存在
//以naem命名的文件夹,若有,则返回其绝对路径,若无
//则返回空串,使用递归算法
//调用时传入所要搜索的文件夹名及所要搜索的初始路径
if(name=="") return"";
if(initpath=="") return "";
DirectoryInfo dir=new DirectoryInfo(initpath);
if(!dir.Exits) return "";
DirectoryInfo[] subdirs=dir.GetDirectories(initpath);
if(subdirs.Length==0) return"";//不存在,返回空串
foreach(DirectoryInfo f in subdirs)
{
if(f.Name==name)
return f.FullName;//返回所要的文件夹的绝对路径
else
return GetPath(Gid,f.FullName);//对每个子目录递归调用
}return "没有找到";
}
foreach(DirectoryInfo f in subdirs)
{
if(f.Name==name)
aaa= f.FullName;//返回所要的文件夹的绝对路径
else
aaa= GetPath(Gid,f.FullName);//对每个子目录递归调用
}
return aaa;
...if(...) aaa="";
foreach(DirectoryInfo f in subdirs)
{
if(f.Name==name)
aaa= f.FullName;//返回所要的文件夹的绝对路径
else
aaa= GetPath(Gid,f.FullName);//对每个子目录递归调用
}
return aaa;
public static string GetPath(string name,string initpath)
{ /*根据name,在初始路径initpath下搜索是否存在
*以naem命名的文件夹,若有,则返回其绝对路径,若无
*则返回空串,使用递归算法
*调用时传入所要搜索的文件夹名及所要搜索的初始路径*/
if(name==""||initpath=="") return"";
DirectoryInfo dir=new DirectoryInfo(initpath);
if(!dir.Exits) return "";
DirectoryInfo[] subdirs=dir.GetDirectories();//原来的函数在这里有错 foreach(DirectoryInfo f in subdirs)
{
if(f.Name==name)
return f.FullName;//返回所要的文件夹的绝对路径
else
return GetPath(name,f.FullName);//对每个子目录递归调用
}
return "";
}