如何将数组中的所有值相乘?

时间:2021-07-27 07:56:22

I have an assignment where I need to find the product of all of the numbers in an array, I'm not sure how to do this.

我有一个任务,我需要找到数组中所有数字的乘积,我不知道如何做到这一点。

    int[] numbers = new int[SIZE];

    Console.WriteLine("Type in 10 numbers");
    Console.WriteLine("To stop, type in 0");
    for (int input = 0; input < SIZE; input++)
    {
        userInput = Console.ReadLine();
        numberInputed = int.Parse(userInput);

        if (numberInputed == ZERO)
        {
            numberInputed = ONE;
            break;
        }
        else
        {
            numbers[input] = numberInputed;
        }

    }

This is where I'm trying to find the product of all of the numbers in the array.

这是我试图找到数组中所有数字的乘积的地方。

    foreach (int value in numbers)
    {
        prod *= value;
    }

    Console.WriteLine("The product of the values you entered is {0}", prod);

What am I doing wrong in the foreach statement? Thanks in advance

我在foreach声明中做错了什么?提前致谢

Edit, left out my declared values

编辑,省略我声明的值

    const int SIZE = 10;
    const int ZERO = 0;
    string userInput;
    int numberInputed;
    int prod = 1;

It now works when I type in all ten values but if I put a 0 in order to break the loop then everything equals 0. How do I prevent a 0 from being entered into the array?

它现在可以在输入所有十个值时工作,但是如果我为了打破循环而放置一个0,那么一切都等于0.如何防止0进入数组?

2 个解决方案

#1


19  

It's possible you initialize prod to 0, which means no matter what numbers are in your array, prod will remain 0. Make sure you initialize it to 1 to get the correct result:

您可以将prod初始化为0,这意味着无论您的数组中有多少数字,prod都将保持为0.确保将其初始化为1以获得正确的结果:

int prod = 1;
foreach (int value in numbers)
{
    prod *= value;
}

You could also use Linq's Aggregate extension method to do the same thing:

你也可以使用Linq的Aggregate扩展方法来做同样的事情:

using System.Linq; // put with other using directives

int prod = numbers.Aggregate(1, (a, b) => a * b);

Update

The real problem (which I failed to notice before) is that your array isn't being fully populated if you break out of your loop early. So any array entries you didn't set are still initialized to 0. To fix this, use a List<int> instead of an int[]:

真正的问题(我之前没有注意到)是你的数组没有完全填充,如果你提前摆脱你的循环。因此,您未设置的任何数组条目仍初始化为0.要解决此问题,请使用List 而不是int []:

using System.Collections.Generic; // put with other using directives

List<int> numbers = new List<int>(SIZE); // Capacity == SIZE

...

for (int input = 0; input < SIZE; input++)
{
    ...
    if (numberInputed == ZERO)
    {
        break;
    }
    else
    {
        numbers.Add(numberInputed);
    }
}

#2


1  

The problem is that you don't keep track of how many items there are in the array that actually are assigned a value. If you exit from the loop using a zero input, then the rest of the items are unchanged. As they are zero by default, you will be using those zeroes in your second loop, and when you have a zero somewhere in the array, the total product becomes zero.

问题是您没有跟踪数组中实际分配了值的项目数。如果使用零输入退出循环,则其余项目保持不变。由于默认情况下它们为零,因此您将在第二个循环中使用这些零,并且当数组中某处为零时,总产品将变为零。

Keep track of how many items there are by keeping the loop variable outside the loop:

通过将循环变量保持在循环外部来跟踪有多少项:

int input = 0;
while (input < SIZE)
{
    userInput = Console.ReadLine();
    numberInputed = int.Parse(userInput);
    if (numberInputed == ZERO) {
      break;
    }
    numbers[input] = numberInputed;
    input++;
}

Now you can use only the items that are actually assigned:

现在您只能使用实际分配的项目:

for (int i = 0; i < input; i++) {
    prod *= numbers[i];
}

#1


19  

It's possible you initialize prod to 0, which means no matter what numbers are in your array, prod will remain 0. Make sure you initialize it to 1 to get the correct result:

您可以将prod初始化为0,这意味着无论您的数组中有多少数字,prod都将保持为0.确保将其初始化为1以获得正确的结果:

int prod = 1;
foreach (int value in numbers)
{
    prod *= value;
}

You could also use Linq's Aggregate extension method to do the same thing:

你也可以使用Linq的Aggregate扩展方法来做同样的事情:

using System.Linq; // put with other using directives

int prod = numbers.Aggregate(1, (a, b) => a * b);

Update

The real problem (which I failed to notice before) is that your array isn't being fully populated if you break out of your loop early. So any array entries you didn't set are still initialized to 0. To fix this, use a List<int> instead of an int[]:

真正的问题(我之前没有注意到)是你的数组没有完全填充,如果你提前摆脱你的循环。因此,您未设置的任何数组条目仍初始化为0.要解决此问题,请使用List 而不是int []:

using System.Collections.Generic; // put with other using directives

List<int> numbers = new List<int>(SIZE); // Capacity == SIZE

...

for (int input = 0; input < SIZE; input++)
{
    ...
    if (numberInputed == ZERO)
    {
        break;
    }
    else
    {
        numbers.Add(numberInputed);
    }
}

#2


1  

The problem is that you don't keep track of how many items there are in the array that actually are assigned a value. If you exit from the loop using a zero input, then the rest of the items are unchanged. As they are zero by default, you will be using those zeroes in your second loop, and when you have a zero somewhere in the array, the total product becomes zero.

问题是您没有跟踪数组中实际分配了值的项目数。如果使用零输入退出循环,则其余项目保持不变。由于默认情况下它们为零,因此您将在第二个循环中使用这些零,并且当数组中某处为零时,总产品将变为零。

Keep track of how many items there are by keeping the loop variable outside the loop:

通过将循环变量保持在循环外部来跟踪有多少项:

int input = 0;
while (input < SIZE)
{
    userInput = Console.ReadLine();
    numberInputed = int.Parse(userInput);
    if (numberInputed == ZERO) {
      break;
    }
    numbers[input] = numberInputed;
    input++;
}

Now you can use only the items that are actually assigned:

现在您只能使用实际分配的项目:

for (int i = 0; i < input; i++) {
    prod *= numbers[i];
}