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
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…