[Solved] Rewriting if elses into switch statement, i have wrote this code with if elses and need to rewrite as switch


You switch it (see what I did there?) by writing something like this:

switch(room.toUpperCase()) {
    case "P":
        //do stuff for P
        break;
    case "S":
        //do stuff for S
        break;
    .....
    default:
        //do what's in your "else" block
        break;
}

The switch statement decides which item to look at, then each case is for each outcome. If none of your cases match, then the code runs the default case. The breaks are pretty important too. If I understand correctly, without the breaks, your code will execute every case’s code beneath and including the matching case. This can be handy if you want that implementation, but it looks like you probably don’t here.

More info on switch-case can be found here:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

3

solved Rewriting if elses into switch statement, i have wrote this code with if elses and need to rewrite as switch