I read that you can use labels to control which loops to break and continue if you have nested loops. I tried doing that in the code below, but when the outer loop is supposed to break when the cabinet is selected, it doesn't and just loops back the dining room loop, only breaking the kitchen loop.
//Same imports used from the original code as needed. import java.net.MalformedURLException; import java.util.Scanner; import javax.swing.JOptionPane; import java.net.URL; import javax.swing.ImageIcon; public class LoopyHauntedHouse { private String name; //Player name. private String location0; //First location selected from door. private String toFrontDoor = ""; private String atFrontDoor; //Method asking for user's name, and welcoming the user as well as asking where they'd like to go for the first time. public void intro()throws MalformedURLException { //URL to image initialized from the original code as needed. URL frontDoor = new URL("http://i.imgur.com/2m3giQk.png"); //Scanner for user input. Scanner scnr = new Scanner(System.in); //Asking for user's name and welcoming the user. name = JOptionPane.showInputDialog(null, "What is your name?"); JOptionPane.showMessageDialog(null, "Welcome " + name + " to the Haunted House!"); //Shows starting location. JOptionPane.showMessageDialog(null, name + " you're at the front door of the haunted house.", "Title", JOptionPane.PLAIN_MESSAGE, new ImageIcon(frontDoor)); //Asks for first choice of room to start at. location0 = JOptionPane.showInputDialog(null, name + " where to next? 'Living Room', 'Dining Room' or 'Stairs'?"); } //Method for the rest of the program allowing users to walk through the house, backtrack, and interact with objects. public void startWalking()throws MalformedURLException { //URLs to images are initialized from the original code as needed. URL stairs = new URL("http://i.imgur.com/WuddJUc.png"); URL bedroom1 = new URL("http://i.imgur.com/HZ6OSyZ.png"); URL bedroom2 = new URL("http://i.imgur.com/JZORNpd.png"); URL bathroom = new URL("http://i.imgur.com/onSEc1J.png"); URL masterBedroom = new URL("http://i.imgur.com/bf4L0sH.png"); URL masterBathroom = new URL("http://i.imgur.com/yp87dTX.png"); URL livingRoom = new URL("http://i.imgur.com/7XQD5Pt.png"); URL bathRoom = new URL("http://i.imgur.com/G0CxjSy.png"); URL diningRoom = new URL("http://i.imgur.com/gyU9mep.png"); URL kitchen = new URL("http://i.imgur.com/tTMRCID.png"); URL pantry = new URL("http://i.imgur.com/zBxPJCs.png"); while(location0.equalsIgnoreCase("Living Room")||(toFrontDoor.equalsIgnoreCase("Living Room"))) { JOptionPane.showMessageDialog(null, name + " you are now in the Living Room"); String move1 = JOptionPane.showInputDialog(null, name + " would you like to explore the 'Chest' walk to the 'Bathroom' or 'Go back' and go to another room?"); if(move1.equalsIgnoreCase("Chest")) { JOptionPane.showMessageDialog(null, name + " a ghost escapes and scares you to death!"); JOptionPane.showMessageDialog(null, "Game Over! You've died.", "Try Again.", JOptionPane.WARNING_MESSAGE, new ImageIcon(livingRoom)); break; } else if(move1.equalsIgnoreCase("Bathroom")) { while(move1.equalsIgnoreCase("Bathroom")) { JOptionPane.showMessageDialog(null, name + " you are now in the bathroom."); String move2 = JOptionPane.showInputDialog(null, name + " would you like to explore the 'Mirror', 'Shower', or 'Go back'?"); if(move2.equalsIgnoreCase("Shower")) { JOptionPane.showMessageDialog(null, name + " the room suddenly steams up and you feel fingers touching the back of your neck..."); } else if(move2.equalsIgnoreCase("Mirror")) { JOptionPane.showMessageDialog(null, name + "you see a bloody face looking back at you!"); } else if(move2.equalsIgnoreCase("Go back")) { break; } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } } else if(move1.equalsIgnoreCase("Go back")) { toFrontDoor = move1; break; } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } outerloop: while(location0.equalsIgnoreCase("Dining Room")) { JOptionPane.showMessageDialog(null, name + " you are now in the Dining Room"); String move1 = JOptionPane.showInputDialog(null, name + " would you like to explore the 'Candelabra', walk to the 'Kitchen', or 'Go back'?"); if(move1.equalsIgnoreCase("Candelabra")) { JOptionPane.showMessageDialog(null, "The candelabra light up by themselves and " + name + " sees a death shadow!"); JOptionPane.showMessageDialog(null, "Game Over! You've died.", "Try Again.", JOptionPane.WARNING_MESSAGE, new ImageIcon(diningRoom)); break; } else if(move1.equalsIgnoreCase("Kitchen")) { while(move1.equalsIgnoreCase("Kitchen")) { JOptionPane.showMessageDialog(null, name + " you are now in the 'Kitchen'."); String move2 = JOptionPane.showInputDialog(null, name + " would you like to explore either the 'Refrigerator', the 'Cabinet', walk to the 'Pantry', or 'Go back'"); if(move2.equalsIgnoreCase("Refrigerator")) { JOptionPane.showMessageDialog(null, name + " opens the refrigerator and finds some delicious soul food."); } else if(move2.equalsIgnoreCase("Cabinet")) { JOptionPane.showMessageDialog(null, "The dished and glasses start flying at you as soon as you open the door. " + name + " gets hit in the head and feels themselves moving towards a light."); JOptionPane.showMessageDialog(null, "Game Over! You've died.", "Try Again.", JOptionPane.WARNING_MESSAGE, new ImageIcon(kitchen)); break outerloop; } else if(move2.equalsIgnoreCase("Pantry")) { while(move2.equalsIgnoreCase("Pantry")) { JOptionPane.showMessageDialog(null, name + " you are now in the Pantry."); String move3 = JOptionPane.showInputDialog(null, name + " would like to explore the 'Dusty Recipe Box', the 'Broom', or 'Go back'?"); if(move3.equalsIgnoreCase("Dusty Recipe Box")) { JOptionPane.showMessageDialog(null, name + "opens it up and a recipe for chocolate devils food cake appears out of no where."); } else if(move3.equalsIgnoreCase("Broom")) { JOptionPane.showMessageDialog(null, "As soon as " + name + " touches the broom, it flies up in the air!"); } else if(move3.equalsIgnoreCase("Go back")) { break; } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } } else if(move2.equalsIgnoreCase("Go back")) { break; } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } } else if(move1.equalsIgnoreCase("Go back")) { toFrontDoor = move1; break; } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } while(location0.equalsIgnoreCase("Stairs")) { JOptionPane.showMessageDialog(null, name + " you are now in font of the stairs."); String move1 = JOptionPane.showInputDialog(null, name + " would you like to walk to 'Bedroom 1', 'Bedroom 2', the 'Master Bedroom', or 'Go back'?"); if(move1.equalsIgnoreCase("Master Bedroom")) { while(move1.equalsIgnoreCase("Master Bedroom")) { JOptionPane.showMessageDialog(null, name + " you are now in the Master Bedroom."); String move2 = JOptionPane.showInputDialog(null, name + " would you like to explore the 'Jewelry Box', walk into the 'Master Bathroom', or 'Go back'?"); if(move2.equalsIgnoreCase("Jewelry Box")) { JOptionPane.showMessageDialog(null, name + " you find the cursed Hope Diamond and feel your doom."); } else if(move2.equalsIgnoreCase("Go back")) { break; } else if(move2.equalsIgnoreCase("Master Bathroom")) { while(move2.equalsIgnoreCase("Master Bathroom")) { JOptionPane.showMessageDialog(null, name + " you are now in the Master Bathroom."); String move3 = JOptionPane.showInputDialog(null, name + " would you like to explore the 'Intricate Oil Lamp', the 'Shower', or 'Go back'?"); if(move3.equalsIgnoreCase("Intricate Oil Lamp")) { JOptionPane.showMessageDialog(null, name + " you rub the lamp and a genie pops out who says he'll grant you 3 wishes!"); } else if(move3.equalsIgnoreCase("Shower")) { JOptionPane.showMessageDialog(null, name + " you suddenly hear singing in the shower but no one is there."); } else if(move3.equalsIgnoreCase("Go back")) { break; } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } } else if(move1.equalsIgnoreCase("Go back")) { toFrontDoor = move1; break; } else if(move1.equalsIgnoreCase("Bedroom 1")) { } else if(move1.equalsIgnoreCase("Bedroom 2")) { } else { JOptionPane.showMessageDialog(null, "Please enter a valid option."); } } } public void toFrontDoor() throws MalformedURLException { if(toFrontDoor.equalsIgnoreCase("Go back")) { atFrontDoor = JOptionPane.showInputDialog(null, name + " where to next? 'Living Room', 'Dining Room', 'Stairs', or 'Leave the house'?"); if(atFrontDoor.equalsIgnoreCase("Leave the house")) { JOptionPane.showMessageDialog(null, "Game Over! Thanks for playing."); } } else { startWalking(); } } }
break outerloopto break the outer loop."The dished and glasses start flying at you..."appear?startWalking()does not appear to be called by anything and how it is called is what we need to know about. Is it called in a loop?