并非所有代碼路徑都返回一個值,執行時會拋出錯誤。請幫助我們盡早解決。似乎有一些代碼路徑沒有返回任何值。有人可以幫忙解決嗎?代碼中有很多for循環。我無法確定是哪一個導致了這個問題。使用系統;使用 System.Collections.Generic;使用 System.IO;使用 System.Linq;使用系統文本;使用 System.Threading.Tasks;//命名空間ConsoleApp7//{類解決方案{static bool CheckElementSymbol(string elementName, string symbol){ symbol = symbol.ToLower(); int symbol_length = symbol.Length; int numberofchars = 0; int firstletter = 0; bool firstcharfound = false; bool secondcharfound = false; //bool symbolfound = false; //int symbolpresent = 0; int secondcharmatch = 0; if (symbol_length == 2) { foreach (char sym in symbol) { numberofchars = numberofchars + 1; var firstcharmatch = new List<int>(); //int index = 0; int sourcelength = elementName.Length; if (numberofchars == 1) { for (int index = 0; index < sourcelength; index++) { int matchfound1stchar = elementName.IndexOf(sym, index, 1); if (matchfound1stchar != -1) { firstletter = 1; firstcharmatch.Add(matchfound1stchar + 1); } }
2 回答

慕森王
TA貢獻1777條經驗 獲得超3個贊
發現了問題。如果字符串沒有字符,那么它應該返回 false
if (symbol_length == 2)
{
foreach (char sym in symbol)(...)//this code is irrelevant.
return false; //here is the solution, if there are no characters in the string, then return false .
}
else
{
return false;
}
下次讓你的代碼更容易閱讀,只顯示相關部分。

qq_笑_17
TA貢獻1818條經驗 獲得超7個贊
首先,請在您的問題中僅發布相關且最少的代碼,以便獲得快速回復。
對于您的查詢,您需要了解您得到的編譯器錯誤是
錯誤 CS0161:“Solution.CheckElementSymbol(string, string)”:并非所有代碼路徑都返回值
這個錯誤是因為你所有的返回語句都在 IF 或 ELSE 語句中。
如果在CheckElementSymbol
方法末尾添加 return 語句,錯誤將得到修復。
return false;
希望這可以幫助。
- 2 回答
- 0 關注
- 79 瀏覽
添加回答
舉報
0/150
提交
取消