我的方法應該檢查以查看我通過的int數組是否有任何負值,如果是這樣,它應該將它們保存到一個字符串中並將它們一起返回他們位於的索引。此時該方法只返回數組的第一個負值。我如何使每個負值和它的索引被添加到一個字符串?如何從C#中的int數組方法返回一個字符串
public static string FindNegative(int[] array)
{
string yes = null;
foreach (var n in array)
{
if (n < 0)
{
yes += (Array.IndexOf(array, n) + ":" + n + ",");
}
return yes;
}
return null;
}
移動你的 '回報是' 出來的foreach循環。 – geedubb