【发布时间】: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