我很难用我编写的以下方法来识别问题。由于错误“并非所有代码路径都返回值”,它当前不会编译:
private static int compareVersionNumbers(string installedVersion, string requiredVersion)
{
if (installedVersion.Equals(requiredVersion))
{
Console.WriteLine("Versions are identical.");
return 0;
}
else if (installedVersion != (requiredVersion))
{
// Split version strings into arrays.
List<string> versionArray = installedVersion.Split('.').ToList<string>();
List<string> otherVersionArray = requiredVersion.Split('.').ToList<string>();
int count = 0;
while ((count < versionArray.Count()) && (count < otherVersionArray.Count()))
{
// Fetch current version component
int currentPart = Convert.ToInt32(versionArray.ElementAt(count));
int otherPart = Convert.ToInt32(otherVersionArray.ElementAt(count));
if (currentPart > otherPart)
{
Console.WriteLine(installedVersion + " is greater than " + requiredVersion);
return 1;
break;
}
else if (currentPart < otherPart)
{
Console.WriteLine(installedVersion + " is less than " + requiredVersion);
return -1;
break;
}
count++;
}
}
}
一般来说,VisualStudio的调试器中是否有任何有用的工具可用于排除无法访问的代码?
答案 0 :(得分:4)
您有一个if
和一个else if
声明。但是你没有else
语句,这意味着如果你的所有条件都是假的,那么你的函数就不会返回值。如果恰好相反,你的elseif
语句改为else
语句您的if
语句(看起来像这样),或返回函数的值结束。
答案 1 :(得分:2)
您遇到的问题意味着编译器至少找到了一种函数不会返回值的方法。这些特殊情况如下:
while
循环后没有返回值。可能是while
甚至无法运行。else if
阻止后else return -1;
答案 2 :(得分:1)
你的函数只在if子句中有2个if子句和return指令。对于第二个if子句,你应该有1个else子句。
答案 3 :(得分:0)
我认为问题是编译器在块中分析你的代码 它找到一个块(内部的if),它不返回所需的整数
此外,如果while的所有内部路径都返回了编译器仍然抱怨的内容,因为else if块不会返回任何内容
让我根据您的代码展示一个人为的例子,其中包含一些修正建议:
int Test()
{
int a = 10;
if(a < 10)
{
// Always false but the compiler is happy here
// because this block returns the integer
return 1;
}
else if(a==10)
{
// We always enter here but the compiler doesn't check at this point
// if the condition is true or not neither what happen inside the while block
// It looks at the else if and conclude that you don't return the integer
while(a < 20)
{
// Again, we know that we always enter the while block but
// the analysis is not executing our code and
// it is already been decided that something is wrong
if(a < 15)
return 2;
else if(a < 18)
return 3;
else
return 4; // Adding this else doens't make any difference
a++;
}
??????? here the compiler wants something to return
}
else
{
// happy here because the block returns the integer
// but, again, adding this else doesn't make any difference
return 5;
}
}
仍然错误没有消失,对于编译器,内部if else不返回整数,而我们的逻辑告诉我们这是不可能的
答案 4 :(得分:0)
原来我只需要在if / if else if块后再返回一次:</ p>
private static int compareVersionNumbers(string installedVersion, string requiredVersion)
{
if (installedVersion.Equals(requiredVersion))
{
Console.WriteLine("Versions are identical.");
return 0;
}
else if (installedVersion != (requiredVersion))
{
// Split version strings into arrays.
List<string> versionArray = installedVersion.Split('.').ToList<string>();
List<string> otherVersionArray = requiredVersion.Split('.').ToList<string>();
int count = 0;
while ((count < versionArray.Count()) && (count < otherVersionArray.Count()))
{
// Fetch current version component
int currentPart = Convert.ToInt32(versionArray.ElementAt(count));
int otherPart = Convert.ToInt32(otherVersionArray.ElementAt(count));
if (currentPart > otherPart)
{
Console.WriteLine(installedVersion + " is greater than " + requiredVersion);
return 1;
break;
}
else if (currentPart < otherPart)
{
Console.WriteLine(installedVersion + " is less than " + requiredVersion);
return -1;
break;
}
count++;
}
}
return 0;
}