N
Neroku
Hello, consider the following code:
public static int sum(int ... num)
{
int sum = 0;
for(int n : num)
sum += n;
return sum;
}
The method works fine (It sums all the numbers of the list), but now If
I declare the n variable outside the for loop as shown below:
public static int sum(int ... num)
{
int sum = 0;
int n;
for(n : num)
sum += n;
return sum;
}
I know the difference between the two declarations: in the first one
the n variable only exits inside the for loop, and in the second
declaration the variable exits in the entire method.
But the second declaration yields a compiler error ("not a statement"),
I don't understand why.
TIA
public static int sum(int ... num)
{
int sum = 0;
for(int n : num)
sum += n;
return sum;
}
The method works fine (It sums all the numbers of the list), but now If
I declare the n variable outside the for loop as shown below:
public static int sum(int ... num)
{
int sum = 0;
int n;
for(n : num)
sum += n;
return sum;
}
I know the difference between the two declarations: in the first one
the n variable only exits inside the for loop, and in the second
declaration the variable exits in the entire method.
But the second declaration yields a compiler error ("not a statement"),
I don't understand why.
TIA