函数返回数组中的最高值

时间:2013-02-04 20:44:58

标签: c# arrays function for-loop

我仍然遇到功能和数组的轻微麻烦。我创建了一个充满随机数的数组。我正在尝试创建一个设置数组并返回最高值的函数。我不确定如何处理这个问题,但到目前为止我写的内容还无法编译。

namespace Task_1._13
{
    class Program
    {
        static void Main(string[] args)
        {
            gettingMaximum(int i);
        }

        public int gettingMaximum(int i);
        {        
            int maximum = 0;
            int[] myArray = new int[10];
            Random rand = new Random();

            for (int i = 0; i < myArray.Length; i++)
            { 
                myArray[i] = rand.Next(19);
            }
            for (int i = 0; i < 10; i++)
            {
                if (i == 0)
                    maximum = myArray[i];
                else
                    if (myArray[i] < maximum) maximum = myArray[i];
                int result = i;
                return result;
            }
        }
    }
}

这就是我到目前为止所得到的。我在编程方面不是很有经验,所以我将不胜感激。

4 个答案:

答案 0 :(得分:1)

for (int i = 0; i < myArray.Length; i++)
        {
            if(myArray[i] > maximum)
           {
             maximum = myArray[i];
           }
        }
return maximum;

或者你可以像这样使用Max function

return myArray.Max();

答案 1 :(得分:1)

如果您想要返回最大值,可以使用System.Linq并使用Max功能。

int largestValue = myArray.Max();

Enunerable.Max Method

答案 2 :(得分:0)

public int gettingMaximum();
{

int maximum = 0;
int[] myArray = new int[10];
Random rand = new Random();

    for (int i = 0; i < myArray.Length; i++)
    {
        myArray[i] = rand.Next(19);
    }
    for (int i = 0; i < myArray.Length; i++)
    {
        if (i == 0)
            maximum = myArray[i];
        else
            if (myArray[i] > maximum) maximum = myArray[i];
    }
    return maximum;
}

这应该有用。

答案 3 :(得分:0)

更新了w / comments

namespace Task_1._13
{
    class Program
    {
        static void Main(string[] args)
        {
            // gettingMaximum(int i); This won't work because the int can't be declared while it's being passed into the method
            int i = 0; // You need to declare the i before you pass it to a method. Although look closely at your method and the reason you are passing in a value. Do you need it or use it?
            Console.WriteLine(gettingMaximum(i)); // I'm outputting the results to the screen so that you can verify that at least something happened
        }

        public static int gettingMaximum(int z)//; You don't need the semicolon here  
                                               // Also, I renamed this to z to demonstrate that you never use it. You could just as easily remove it
                                               // Also, this method must be marked static which is outside the scope of this question
        {
            int maximum = 0;
            int[] myArray = new int[10];
            Random rand = new Random();

            // This looks good. You create the array and loop through the items and set the value
            for (int i = 0; i < myArray.Length; i++)
            { 
                myArray[i] = rand.Next(19);
            }

            // This code counts to 10. During each step, it compares the current step with 0. 
            // If the step (i) is 0, you set the maximum to to that value.
            // Otherwise, you see if the array value at that step is LESS than the current maximum, and if it is
            // you set the maximum to be that value. 
            //for (int i = 0; i < 10; i++) 
            //{
            //    if (i == 0)
            //        maximum = myArray[i];
            //    else
            //        if (myArray[i] < maximum) maximum = myArray[i];
            //    int result = i;   // Here, you're actually setting the result to the current step number
            //    return result;   // I think you meant to set it to maximum, but you can leave it out completely and just return maximum. 
            //}

            for (int i = 0; i < 10; i++)
            {
                if (i == 0)
                {
                    maximum = myArray[i];
                }
                else
                { // Since you have nested if's, i'm using braces to illustrate the paths of the branches
                    if (myArray[i] > maximum) // What i think you mean is, is the value at this item in the array MORE than my current maximum
                    {
                        maximum = myArray[i];
                    }
                }
            }

            return maximum;
        }
    }
}