在java中输入某个单词时跳回代码行(goto函数)

Jump back to a line in code(goto function) when a certain word is entered in java

所以我需要一些代码来实现,如果输入四个单词中的一个,它就会跳回到某一行。这是基于文本的角色扮演游戏。

if(input3.equalsIgnoreCase("Inside")) {                        
System.out.println("------------------------------------------");
System.out.println("\n- You go inside the tent right as the sky becomes a dark shade, zipping yourself shut inside. -");
System.out.println("- You scan over the things in the tent. -");
System.out.println("- There is a lantern in the corner, a sleeping bad next to it, and a walkie talkie on the pillow. -\n");
System.out.println("-----------------------------------------------");    
System.out.println("\t\nWhat would you like to do?\n");
System.out.println("\tSleep. Get into the sleeping bag and go to sleep.");
System.out.println("\tCall. Use the walkie talkie to try to reach out to someone.");
System.out.println("\tIntrospect. Think about what is going on and how you are here.");
System.out.println("\tOutside. Go outside the tent.\n");
String input4 = in.nextLine();
if(input4.equalsIgnoreCase("Sleep")) {
System.out.println("-----------------------------------------------");
System.out.println("\n- You move the walkie talkie and get into the sleeping bag. You slowly drift off into a deep slumber. -\n");
}
else if(input4.equalsIgnoreCase("Call")) {
System.out.println("-----------------------------------------------");
System.out.println("\n- You grab the walkie talkie and press a couple of buttons, trying to figure out how it works. -");
System.out.println("- After a little bit of fiddling with it, you give up, thinking it's ran out of battery. -");
System.out.println("- You feel your eyes droop and decide to get into the sleeping bad and go to sleep. -\n");
}
else if(input4.equalsIgnoreCase("Introspect")) {
System.out.println("-----------------------------------------------");
System.out.println("\n- You huddle into the corner, thinking. -\n");
System.out.println("- 'How are you here?' Your mind wanders. -");
System.out.println("- 'Why are you here?' You don't know. -");
System.out.println("- 'You don't even know what you look like. -");
System.out.println("- Before long, your eyes slowly close shut, as you head into a deep slumber. -\n");
}
else if(input4.equalsIgnoreCase("Outside")) {
System.out.println("-----------------------------------------------");
System.out.println("\n- You unzip the tent and step outside. -");
System.out.println("- A cold shudder washes over you, making you quickly go back inside, zipping yourself back inside. -\n");
}

我基本上想要它,以便当用户键入“Outside”时,此函数...

else if(input4.equalsIgnoreCase("Outside")) {
    System.out.println("-----------------------------------------------");
    System.out.println("\n- You unzip the tent and step outside. -");
    System.out.println("- A cold shudder washes over you, making you quickly go back inside, zipping yourself back inside. -\n");
    }

...是 运行 并且在打印行之后我希望它跳回到..

if(input3.equalsIgnoreCase("Inside")) {                        
System.out.println("------------------------------------------");
System.out.println("\n- You go inside the tent right as the sky becomes a dark shade, zipping yourself shut inside. -");
System.out.println("- You scan over the things in the tent. -");
System.out.println("- There is a lantern in the corner, a sleeping bad next to it, and a walkie talkie on the pillow. -\n");
System.out.println("-----------------------------------------------");  

...和 ​​运行 之后的所有内容。

您可以做的是将所有内容放入一个 while 循环中。简化版:

if(input3.equalsIgnoreCase("Inside")) {
    while(true) {
        System.out.println("Some text");
        System.out.println("Some options:");
        String input4 = in.nextLine();
        if(input4.equalsIgnoreCase("Sleep")) {
            System.out.println("Some text");
            break;
        } else if(input4.equalsIgnoreCase("Outside") {
            System.out.println("Some text");
        }
    }
}

通常我会说 while(true) 是不好的做法,但我认为在这种情况下没问题。