I am learning switch statements. I write some codes like this:
I was asked to add "case" or "default" or '}' after I compiled my code. I tried to change code "case 2: " to "default:", it worked. I was told that the compiler would check the type of all case options without the actual values. Is it right? Should I add 'defaultl' always?
default is an optional clause for the switch statement so, no, you don't need to always add it.
Think of the default clause as the last "else" of a series of if-else-if statements. That means it catches "everything else that wasn't caught by the previous conditions". If you don't provide it, then nothing will happen when none of the conditions match. In your case, for example, nothing happens if computerMove is 3 or 4.
More specifically, if your switch statement has no default clause, computerMove could remain uninitialized. Since a local variable must be definitely assigned a value, the compiler will notice that and will report an error telling you that the System.out.println() statement could still be dealing with an uninitialized variable, which is not allowed.