【问题标题】:How to multiply all values in an array?如何将数组中的所有值相乘?
【发布时间】:2013-11-21 22:02:11
【问题描述】:

我有一个任务,我需要找到一个数组中所有数字的乘积,我不知道该怎么做。

    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;
        }

    }

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

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

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

我在 foreach 语句中做错了什么?提前致谢

编辑,省略我声明的值

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

当我输入所有十个值时它现在可以工作,但是如果我输入一个 0 以打破循环,那么一切都等于 0。如何防止将 0 输入到数组中?

【问题讨论】:

  • 你的代码出了什么问题?

标签: c# arrays multiplication


【解决方案1】:

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

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

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

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

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

更新

真正的问题(我之前没有注意到)是如果你提前跳出循环,你的数组没有被完全填充。因此,您未设置的任何数组条目仍会初始化为 0。要解决此问题,请使用 List&lt;int&gt; 而不是 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);
    }
}

【讨论】:

  • +1。您总是设法提供不止一种做事方式。荣誉:)
  • 对不起,我用声明的值更新了我的帖子。我确实有 prod 等于 1。我尝试了你的方法,但我得到一个“错误 1'System.Array' 不包含'Aggregate' 的定义并且没有扩展方法'Aggregate' 接受'System.Array' 类型的第一个参数可以找到(您是否缺少 using 指令或程序集引用?)”错误
  • 使用 System.Linq;
  • @user2781666 你确定你没有声明一个名为prod 的局部变量来隐藏类成员吗?同样要使用Aggregate,请确保在文件顶部包含using System.Linq
  • 不幸的是,我不确定你的意思,我只有 int prod = 1;我将在哪里添加 System.Linq?现在我正在使用 System;
【解决方案2】:

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

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

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

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

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

【讨论】:

    【解决方案3】:

    将数组中的所有数字相乘

    int[] array = { 1, 2, 3, 4, 5 };
    int sum = array[0];
    for (int i = 1; i != array.Length; i++)
    {
        sum *= array[i];
    }
    

    如果您的数组以某种方式填充了零 (0),则使用 List 而不是数组。

    【讨论】:

    • 您的循环是正确的,ArrayFormula,但它不是问题的答案。没有固定数量的元素。元素的数量由用户决定,最后一项输入“0”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多