The correct program fragment is given below.
Here is the corrected program.
It would be OK if you put a break
after the statment in the default case,
but it is not needed.
class Switcher { public static void main ( String[] args ) { char color = 'Y' ; String message = "Color is"; switch ( color ) { case 'R': message = message + " red" ; break; case 'O': message = message + " orange" ; break; case 'Y': message = message + " yellow" ; break; case 'G': message = message + " green" ; break; case 'B': message = message + " blue" ; break; case 'V': message = message + " violet" ; break; default: message = message + " unknown" ; } System.out.println ( message ) ; } }
Often what you really want is for several characters to select a single case. This can be done using several case statments, followed by just one list of statments. For example, here both 'y' and 'Y' select the same statement:
case 'y': case 'Y': message = message + " yellow" ; break;