[英]Function returning the highest value in an array
I am still having slight troubles with functions and arrays. 功能和数组仍然有些麻烦。 I have created an array that has been filled with random numbers.
我创建了一个充满随机数的数组。 I am trying to create a function that sets up the array and returns the highest value.
我正在尝试创建一个设置数组并返回最高值的函数。 I am not to sure on how to approach this but this and what I have written so far has not been able to compile.
我不确定如何处理这个问题,但是到目前为止我写的内容还无法编译。
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;
}
}
}
}
That's what I have gotten so far. 这就是我到目前为止所得到的。 I am not very experienced in programming so help would be appreciated.
我在编程方面不是很有经验,所以我将不胜感激。
for (int i = 0; i < myArray.Length; i++)
{
if(myArray[i] > maximum)
{
maximum = myArray[i];
}
}
return maximum;
OR you can just use the Max function like this 或者你可以像这样使用Max函数
return myArray.Max();
If you want to return the largest value, you can use System.Linq
and use the Max
function. 如果要返回最大值,可以使用
System.Linq
并使用Max
函数。
int largestValue = myArray.Max();
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;
}
This should work. 这应该工作。
Updated 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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.