Eduardo Morales Eduardo Morales - 1 month ago 11
Java Question

JAVA: Why isn't my Switch Statement working?

Why isn't this switch statement changing the values of the object? For some reason when it runs any case, this.(anything) = (anything) doesn't change the value of the object. I want for example, in case "Living Room", this.upNext1 = "Pantry".

public class HauntedHouse {
public static class room{
String previousRoom = "";
String currentRoom = "Front Door";
String upNext1 = "Living Room";
String upNext2 = "Dining Room";
String upNext3 = "Stairs";
String object1 = "";
String object2 = "";
public room(){};
public void room(String currentRoom){
switch(currentRoom){
case "Front Door":
this.previousRoom = this.currentRoom;
this.currentRoom = "Front Door";
this.upNext1 = "Living Room";
this.upNext2 = "Dining Room";
this.upNext3 = "Stairs";
this.object1 = "";
this.object2 = "";
case "Living Room":
this.previousRoom = this.currentRoom;
this.currentRoom = "Living Room";
this.upNext1 = "Pantry";
this.upNext2 = "1st Floor Bathroom";
this.upNext3 = "";
this.object1 = "Chest";
this.object2 = "";
case "Dining Room":
this.previousRoom = this.currentRoom;
this.currentRoom = "Dining Room";
this.upNext1 = "Kitchen";
this.upNext2 = "";
this.upNext3 = "";
this.object1 = "Candelabra";
this.object2 = "";
case "Stairs":
this.previousRoom = this.currentRoom;
this.currentRoom = "Dining Room";
this.upNext1 = "Bedroom 1";
this.upNext2 = "Bedroom 2";
this.upNext3 = "Master Bedroom";
this.object1 = "Candelera";
this.object2 = "Shower";
case "1st Floor Bathroom":
this.previousRoom = this.currentRoom;
this.currentRoom = "1st Floor Bathroom";
this.upNext1 = "";
this.upNext2 = "";
this.upNext3 = "";
this.object1 = "Mirror";
this.object2 = "Shower";
case "Pantry":
this.previousRoom = this.currentRoom;
this.currentRoom = "Pantry";
this.upNext1 = "";
this.upNext2 = "";
this.upNext3 = "";
this.object1 = "Dusty recipe box";
this.object2 = "Broom";
case "Kitchen":
this.previousRoom = this.currentRoom;
this.currentRoom = "Kitchen";
this.upNext1 = "Pantry";
this.upNext2 = "";
this.upNext3 = "";
this.object1 = "Refrigerator";
this.object2 = "Cabinet";
case "Bedroom 1":
this.previousRoom = this.currentRoom;
this.currentRoom = "Bedrrom 1";
this.upNext1 = "2nd Floor Bathroom";
this.upNext2 = "";
this.upNext3 = "";
this.object1 = "Rocking Chair";
this.object2 = "Window";
case "2nd Floor Bathroom":
this.previousRoom = this.currentRoom;
this.currentRoom = "2nd Floor Bathroom";
this.upNext1 = "Bedroom 2";
this.upNext2 = "";
this.upNext3 = "";
this.object1 = "Mirror";
this.object2 = "Shower";
case "Bedroom 2":
this.previousRoom = this.currentRoom;
this.currentRoom = "Bedroom 2";
this.upNext1 = "Master Bedroom";
this.upNext2 = "Bedroom 1";
this.upNext3 = "";
this.object1 = "Doll House";
this.object2 = "Dresser";
case "Master Bedroom":
this.previousRoom = this.currentRoom;
this.currentRoom = "Master Bedroom";
this.upNext1 = "Master Bathroom";
this.upNext2 = "";
this.upNext3 = "";
this.object1 = "Jewelry Box";
this.object2 = "";
case "Master Bathroom":
this.previousRoom = this.currentRoom;
this.currentRoom = "Master Bathroom";
this.upNext1 = "";
this.upNext2 = "";
this.upNext3 = "";
this.object1 = "Intricate Oil Lamp";
this.object2 = "Shower";
}

Answer

You have to add break to each case, e.g.

switch(choice){
   case 1:
     System.out.print("haha");
     break;
   case 2:
     System.out.print("aloha");  
     break;
}

Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.