So I've seen a bunch of other posts about this, but they didn't apply since a) I'm trying to return to the main method while SKIPPING a while loop inside of the main. I'm making a text adventure game and it works in steps (separate methods), by calling the next step from within the step at the end. for example, the first step is EastStoryline1()
, and the second is EastStoryline2()
, and at the end of the code for EastStoryline1()
, it says "EastStoryline2()"
.
So the actual main is pretty small, since it's just one method looping into the next. There are also 2 while loops in the main. The first comes right after I establish the scanner and boolean playagain, which basically surrounds the rest of the main starts the game while playagain = true. the second loop comes right after the first, which basically says while def (the players health) > 0, play the events of the game. After the second loop, but still in the first loop, the code calls the method Die()
, and then asks the player whether they want to play the game.
SO basically, what code do I put inside Die()
in order to break out of any existing loop chain and bring it to the next code after Die()
is called in the main. The problem is that I also use Die()
in other methods, and each time it's called I want it to return to the code after Die()
in the main. Here's the code for the main (sorry for bad formatting):
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
boolean playagain = true;
while(playagain == true)
{
while(def > 0)
{
TitleScreen("TXT ADVENTURE!");
System.out.println("Pick a character: Rogue, Paladin, or Priest
(capitals DO matter!)");
String character = keyboard.next();
CharacterChoice(character);
System.out.println("You wake up on a dusty road, with no memory of who
you are or how you got here. You can only remember your name, and how to
fight. To the east lies a small dock with a boat. To the west, there seems
to be a sand-scarred mountain range. Do you go east, or west?");
String ew = keyboard.next();
EastWest(ew);
}
Die();
System.out.println("Do you want to play again?");
String playornah = keyboard.next();
if(playornah.equals("yes") || playornah.equals("Yes"))
{
playagain = true;
}
else
{
playagain = false;
}
}
}
And this was the code for Die I used system.exit(0)
, but now I want it to return to the main after Die is called there instead of just ending the program):
public static void Die()
{
System.out.println("GAME OVER");
System.exit(0); //I tried using break but it wouldn't compile
}
So what to I code in Die()
in order for (no matter where it's called) return to the main after the spot where Die()
is called.