简体   繁体   English

C#如何在方法中使用部分填充的数组?

[英]C# how do I use a partially filled array in a method?

My instructor wants us to create an array of 10 elements and then fill it with random numbers using console input and then multiply all the numbers together to get the product. 我的老师希望我们创建一个包含10个元素的数组,然后使用控制台输入用随机数填充它,然后将所有数字相乘得到乘积。 I can't figure out how to pass the array to a method and then successfully get the product. 我不知道如何将数组传递给方法,然后成功获取产品。 Do I use a for loop? 我是否使用for循环? I don't think I can use a foreach loop because it could be partially filled. 我认为我不能使用foreach循环,因为它可能会被部分填充。 How do I even pass the array to the method in the first place? 我如何首先将数组传递给方法?

Here's what I have so far to store the values in the array. 到目前为止,这是将值存储在数组中的内容。

for (int i = 0; i < SIZE; i++)
{

    bool valid = false;
    do
    {
         Write("Enter an integer value or 0 to stop ");
         string input = ReadLine();
         valid = int.TryParse(input, out userInput);
    } while (!valid);

    if (userInput == 0)
    {
         break;
    }
    array[i] = userInput;
}

product = p.ArrayProduct(array.Length);

The method for multiplying is as below: 乘法的方法如下:

public int ArrayProduct(int[] array)
{
   int p = 1;
   for(int i = 0; i < array.Length; i++)
   {
       p *= array[i];
   }
   return p;
}

You can multiply in foreach loop as well: 您也可以在foreach循环中相乘:

public int ArrayProduct(int[] array)
{
   int p = 1;
   foreach(int element in array)
   {
       p *= element;
   }
   return p;
}

Or you can use Array class like below: 或者您可以使用如下所示的Array类:

public int ArrayProduct(int[] array)
{
    int p = 1;
    Array.ForEach(array, el =>
    {
        p *= el;
    });
    return p;
}

There is another extension method for arrays called, gues how! 数组还有另一种扩展方法,请问如何! ForEach

public int ArrayProduct(int[] array)
{
    int p = 1;
    array.ForEach(element =>
    {
        p *= element;
    });
    return p;
}

Then you call it after you have the data as below: 然后,您在拥有以下数据之后调用它:

int product = ArrayProduct(array);
Console.WriteLine(product);

There is another way as @Peter A. Schneider said by using Linq according this question here 还有一种方式@Peter A.施奈德说,通过使用Linq ,根据这个问题, 在这里

public int ArrayProduct(int[] array)
{
    return array.Aggregate(1, (acc, val) => acc * val);
}

better in c# 6 and above: c# 6和更高版本中更好:

public int ArrayProduct(int[] array) => array.Aggregate(1, (acc, val) => acc * val);

If you have problem with getting the data please let me know! 如果您在获取数据时遇到问题,请告诉我!

You can call a method by reference. 您可以通过引用来调用方法。 You should use from out and ref key word: https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx 您应该使用out和ref关键字: https : //msdn.microsoft.com/en-us/library/t3c3bfhx.aspx

Here's an example of tracking the number of entries in a separate variable and passing that to the processing function along with the array: 这是一个跟踪单独变量中的条目数并将其与数组一起传递给处理函数的示例:

static void Main(string[] args)
{
    int SIZE = 10;
    int numEntries = 0;
    int[] array = new int[SIZE];

    Console.WriteLine("Enter up to " + SIZE.ToString() + " integer(s).");

    string input;
    int userInput;
    for (int i = 0; i < array.Length; i++)
    {
        bool valid = false;
        do
        {
            Console.Write("Enter integer #" + (i+1).ToString() + " or 0 to stop: ");
            input = Console.ReadLine();
            valid = int.TryParse(input, out userInput);
            if (!valid)
            {
                Console.WriteLine("Invalid entry!");
            }
        } while (!valid);

        if (userInput == 0)
        {
            break;
        }

        numEntries++;
        array[i] = userInput;
    }

    if (numEntries > 0)
    {
        int product = ArrayProduct(array, numEntries);
        Console.WriteLine("The product is " + product.ToString("n0") + ".");
    }
    else
    {
        Console.WriteLine("No entries were made!");
    }

    Console.Write("Press [Enter] to quit.");
    Console.ReadLine();
}

private static int ArrayProduct(int[] arr, int length)
{
    int product = 1;
    if (length <= arr.Length)
    {
        for(int i = 0; i < length; i++)
        {
            product = product * arr[i];
        }
    }
    return product;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM