Har*_*ell 3 c# xcode syntax-error monodevelop
int search(string [][]mat, int n, string x){
//set indexes for top right element
for(int i = 0; i<n; i++)
{
for(int j = n-1; j>=0; j--)
{
if ( mat[i][j] == x )
{
Debug.Log(x +""+"Found at "+i +" "+j);
// int[] n2 = new int[] {2, 4, 6, 8};
// int [] xyz = new int [] {i, j};
return i;
}
}
}}
Run Code Online (Sandbox Code Playgroud)
如何摆脱这个错误:并非所有代码路径都返回一个值?
错误: *Assets/Scripts/Chess/Bishop.cs(237,22):错误CS0161:`Bishop.search(string [] [],int,string)':并非所有代码路径都返回值*
找出你想要发生的事情,如果你找不到x
,并在方法结束时返回.例如:
// Fixed naming conventions and indentation...
// Why do we need n here at all? Why not just use the length of the array?
int Search(string[][] mat, int n, string x)
{
//set indexes for top right element
for (int i = 0; i < n; i++)
{
// Why are we looking backwards here?
for (int j = n - 1; j >= 0; j--)
{
if (mat[i][j] == x)
{
// More readable formatting...
Debug.Log(string.Format("{0} found at {1}, {2}", x, i, j));
return i;
}
}
}
// Not found: return -1 to indicate failure. Or you could throw an exception
return -1;
}
Run Code Online (Sandbox Code Playgroud)
更一般地说:编译器错误消息在这里相当清楚 - 有一种方法可以在不返回任何内容的情况下到达方法的末尾.值得退后一步,试着想一想为什么你自己不能解决这个问题.您是否对编译器错误消息给予了足够的重视?在所有情况下,你是否通过该方法可能做的所有事情?你下次怎么能更好地处理好这件事?
归档时间: |
|
查看次数: |
1494 次 |
最近记录: |