; at the end of the for loop, it is taken as an empty statement, the equivalent of an empty block for your for loop. The compiler reads your code as:
int i; .... for(i=0; i<9; i++) ; { System.out.println ("Please enter a number:"); Num[i] = keyboard.nextDouble(); Sum += Num[i]; Product *= Num[i]; }
Remove ; to get your intentional behavior.
If you do not need i outside the loop, you can transfer its declaration to the for statement.
for(int i=0; i<9; i++) { // `i` is only usable here now } // `i` is now out of scope and not usable
Using this syntax when an erroneous semicolon was present ; , there would be a compilation error that previously warned you of the error ; . The compiler will see this:
for(int i=0; i<9; i++) ; { System.out.println ("Please enter a number:"); Num[i] = keyboard.nextDouble();
This would be an example of why good practice is to limit the scope of variables when possible.
Bert f
source share