有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 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一书解释了如何),那么就可以更容易地看到逻辑(以及逻辑中的错误)

    我发现写一个简单的例子来说明这个问题通常是解决这类奇怪问题的好方法。如果你发现这个简单的例子有效,你可以逐渐增加复杂性,直到它再次崩溃,然后你就可以确切地知道是什么导致了它