0

我仍然对函数和数组有一些小问题。我创建了一个填充了随机数的数组。我正在尝试创建一个设置数组并返回最大值的函数。我不确定如何处理这个问题,但是这个和我到目前为止所写的内容都无法编译。

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

4 回答 4

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

或者你可以像这样使用Max 函数

return myArray.Max();
于 2013-02-04T20:49:09.743 回答
1

如果要返回最大值,可以使用System.Linq和使用Max函数。

int largestValue = myArray.Max();

Enunerable.Max 方法

于 2013-02-04T20:49:22.740 回答
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;
}

这应该有效。

于 2013-02-04T20:51:01.823 回答
0

更新了评论

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;
        }
    }
}
于 2013-02-04T21:16:00.800 回答