2

I am trying to make a simple text adventure game in Java and when I type Quit it ends the game. But instead it will ask if you want to do the quest even if you type quit which is suppose to end the code, I tried using loops to fix this error but had to luck. Any idea where I went wrong?

import java.util.*;

public class TextGame {
    public static void main(String[] args) {

        //Boolean run = true;
        //while (run){

        Scanner in = new Scanner(System. in );

        System.out.println("~SPECTRE~");
        System.out.println("");
        System.out.println("Please Choose One Below");
        System.out.println("");
        System.out.println("New");
        System.out.println("Quit");
        System.out.println("");

        String option;
        System.out.print("Select one of the options here: ");
        option = in .next();

        if (option.equals("New")) {
            System.out.println("Hello adventurer! Welcome to the land of Spectre.");

            String name;
            System.out.print("What is your name adventurer? ");
            name = in .next();
            System.out.println("Hello there! " + name);
        } else if (option.equals("Quit")) {
            System.out.println("*Returns to desktop*");
        }

        String quest;
        System.out.print("Would you like to go on a quest? ");
        quest = in .next();

        if (quest.equals("Yes")) {
            System.out.println("Here is a list of quests that I would like you to do ");
            System.out.println("");
            System.out.println("1) Fight the evil troll of Port Howlham");
            System.out.println("");
            System.out.println("2) Deliver supplys to the soliders in need");
            System.out.println("");
            System.out.println("3) Find the Kings lost son");
            System.out.println("");
            System.out.println("4) Find the Gemstone of Darlingbee to defeat the evil witch of Hobbitstone");

        } else {
            option.equals("");
            System.out.println("*Please pick New Game or Quit*");
        }
    }
}
4
  • Of course a program will continue processing instructions unless it has an instruction to stop. Commented Mar 1, 2014 at 5:37
  • I know that, but I didn't the code to do so since i'm new to Java. Commented Mar 1, 2014 at 5:55
  • It's just the way you asked the question--as if the program would terminate without being instructed to do so. So I felt the need to point out this fact since you'd be surprised the things some people don't have an understanding of. ; ) Commented Mar 1, 2014 at 6:02
  • Ah yeah. I see what you mean. Thanks a lot! :) Commented Mar 1, 2014 at 6:09

3 Answers 3

6

try this:

} else if (option.equalsIgnoreCase("Quit")) {
    System.exit(0);
}

Or this:

} else if (option.equalsIgnoreCase("Quit")) {
    return;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I never knew this. Thanks for your help. Since i'm new to Java I really appreciate your help,
2

You're probably looking to exit the program. Use System.exit(0);

 else if(option.equals("Quit"))
{
    System.exit(0);
}

The 0 argument is a status code that indicates normal termination of the program.

Comments

1

You can use System.exit(code) to finish the program. Put this at the end of the else if block:

if (option.equals("Quit") {
    ...
    System.exit(0);
}

The code can be any integer:

The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.