-2

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.

Alon
  • 687
  • 1
  • 6
  • 10
  • Just get to the end of the method, or use a `return` statement... – Jon Skeet Oct 11 '18 at 17:00
  • Break can be used only in loops or switch statements. System.exit(0); is used if you want to come out of the program completely without further execution. Comment out // System.exit(0); from your die method, It should solve the problem. – Vinayak Dornala Oct 11 '18 at 17:19

2 Answers2

2
public static void Die()
{
    System.out.println("GAME OVER");
    System.exit(0); //I tried using break but it wouldn't compile
}

System.exit(0); ends the program. If you want to just end the method either:

  • Let the method end naturally when it has no more statements. (Just remove System.exit(0);)

  • Replace System.exit(0); with a return; statement

From the docs:

A method returns to the code that invoked it when it

  • completes all the statements in the method,
  • reaches a return statement, or
  • throws an exception (covered later),

whichever occurs first.

Community
  • 1
  • 1
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
0

You can't return from an external method, How ever you can use a try-finally to return it once you have run your code.

SamHoque
  • 2,978
  • 2
  • 13
  • 43