The error message is because you're declaring int multip
in the innermost scope of the for loop. The int goes out of scope after that, and is not usable. If you want multip
to be accessible outside of the for
loop, declare it outside of the for loop:
static void letsMult(int [] myArray)
{
int multip = 1;
for (int a = 0; a < myArray.Length; a++ )
{
for (int b = a+1; b < myArray.Length; b++ )
{
multip = myArray[a] *= myArray[b];
}
}
Console.WriteLine(multip);
}
Note, it's declared above the for loop, but used in the loop, and outputted outside the loop, but still in its scope (which in my case is "the entire function").
You still have a logical issue. Are you sure you need a nested loop? How would you describe to a person how to multiply a list of numbers to get the total product?