注册 登录
编程论坛 C# 论坛

求各位指点这段调用函数的代码哪错了

causticsoda 发布于 2017-02-27 17:01, 1460 次点击
namespace ConsoleApplication8
{
    class program
    {
        static int MaxValue(int[] intArray, out int maxIndex)
        {
            int maxVal = intArray[0];
            maxIndex = 0;
            for (int i = 1; i < intArray.Length; i++)
            {
                if (intArray[i] > maxVal)
                {
                    maxVal = intArray[i];
                    maxIndex = i;
                }
                return maxVal;
            }
        }
        static void Main(string[] args)
        {
            int[] myArray = { 1, 8, 3, 6, 2, 5, 9, 3, 0, 2 };
            int maxIndex;
            Console.WriteLine("The maximum value in myArray is {0}",MaxValue(myArray,out maxIndex));
            Console.WriteLine("The first occurrence of this value is at element {0}", maxIndex + 1);
        
        }
        //I dont know where the problems lies.
    }
}
-------------------------------------------------------------------------------------------------------------
功能是找出最大值。
报错窗口
not all code paths return a value.

感激不尽!
1 回复
#2
yhlvht2017-03-02 00:24
如果代码没有进入for循环,这时该怎么办呢,提示你并非所有代码路径都有返回值
1