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

Time:02-17

So I need some line of code to make it so that if one of the four words is entered, it jumps back to a certain line. This is for a text-based rpg game.

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");
}

I basically want it so that when the user types "Outside", this function...

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");
    }

...is run and after the print lines I want it to jump back to..

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("-----------------------------------------------");  

...and run everything after it again.

CodePudding user response:

What you could do is put everything inside a while loop. A simplified version:

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");
        }
    }
}

Normally I would say that while(true) is bad practice but I think in this case it's ok.

  • Related