Java标签循环中断

时间:2014-11-15 17:26:44

标签: java

我读过你可以使用标签来控制哪些循环中断并在你有嵌套循环时继续。我尝试在下面的代码中执行此操作,但是当选择了机柜时外部循环应该断开,它不会只是循环回到餐厅循环,只会打破厨房循环。

//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();
    }
}
}

1 个答案:

答案 0 :(得分:1)

您对标签的理解是正确的。 break outerloop语句应该打破外部循环,如此示例所示:

public class Example {

    public static void main(String[] args) {
        outerLoop: while (true) {
            int i = 0;
            while (true) {
                System.out.println(i++);
                if (i > 5) {
                    break outerLoop;
                }
                if (i > 10) {
                    break;
                }
            }
            System.out.println("Broken inner loop");
        }
        System.out.println("Broken outer loop");
    }
}

哪个输出:

0
1
2
3
4
5
Broken outer loop

我怀疑您的代码中的break outerloop语句实际上并未运行。或者另一个循环在您发布的代码中不可见,导致外循环再次运行?

我建议简化代码(例如extracting some methods)和/或使用调试器逐步完成代码。在142行,您的startWalking()方法开始变得笨拙。如果你将你的方法保持在10行以下(Martin Fowler的书Refactoring解释了如何),它会让事情变得更容易看到逻辑(以及逻辑中的错误)。

我发现写一个更简单的例子来证明问题通常是解决这些奇怪问题的好方法。如果您发现这个简单的示例有效,您可以逐渐添加复杂性,直到它再次中断,然后您确切知道导致它的原因。