There are various ways to achieve what you need. I'll outline a few.
Use methods
This is simple, and somewhat clean and easy to maintain. All you need to do is wrap the code you would like to execute in each case in a separate method then call those methods from within the switch. This is exactly what functions are for.
switch(option){ case 1: doA(); break; case 2: doB(); break; ... // other cases ... case 9: doA(); doB(); ... // other method calls ... break;}
Switch to if
statements
This is pretty self explanatory, just check if the option is each different case or option 9.
if(option == 1 || option == 9){ do A;}if(option == 2 || option == 9){ do B;}...// other cases...
(Mis)use break
s
This is fairly ugly and I wouldn't recommend it but it's really up to personal preference (and how easy to read and maintain you want the code to be in the future).
If option
is 9
, then we flip a flag to turn off all breaks in the switch statement. This effectively makes all other cases below it just execute linearly (as the breaks to leave the switch are disabled).
boolean isCase9 = false;switch(option){ case 9: isCase9 = true; case 1: doA(); if(!isCase9){ break; } case 2: doB(); if(!isCase9){ break; } ... // other cases ...