1
public static void main(String[] args) {
    StackX theStack = new StackX(99);

    String current = "Enterprise";
    int logOut = 1;
    String planetName;
    int secretCode;
    //This is the start out, do not loop this
    System.out.println("You are on the enterprise, where would you like to teleport?");

    //while loop will be done after reaching Io
    do {

        Scanner input = new Scanner(System.in);
        //Grab input from user
        System.out.println("Enter a planet name: ");
        planetName = input.nextLine();
        System.out.println("Enter a the four digit code for " + planetName + ": ");
        secretCode = input.nextInt();

        if (current.equals("Enterprise")){

            if (planetName.equalsIgnoreCase("Europa") && secretCode == 9007){
                current = planetName;
                Moon Europa = new Moon(planetName, secretCode);
                theStack.push(Europa);
            } else if (planetName.equalsIgnoreCase("Titan") && secretCode == 1232){
                current = planetName;
                Moon Titan = new Moon(planetName, secretCode);
                theStack.push(Titan);
            } else if (planetName.equalsIgnoreCase("Rhea") && secretCode == 5623){
                current = planetName;
                Moon Rhea = new Moon(planetName, secretCode);
                theStack.push(Rhea);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Titan")){

            if (planetName.equalsIgnoreCase("Enterprise") && secretCode == 1212){
                current = planetName;
                Moon Enterprise = new Moon(planetName, secretCode);
                theStack.push(Enterprise);
            } else if (planetName.equalsIgnoreCase("Rhea") && secretCode == 5623){
                current = planetName;
                Moon Rhea = new Moon(planetName, secretCode);
                theStack.push(Rhea);
            } else if (planetName.equalsIgnoreCase("Elara") && secretCode == 1264){
                current = planetName;
                Moon Elara = new Moon(planetName, secretCode);
                theStack.push(Elara);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Rhea")){

            if (planetName.equalsIgnoreCase("Enterprise") && secretCode == 1212){
                current = planetName;
                Moon Enterprise = new Moon(planetName, secretCode);
                theStack.push(Enterprise);
            } else if (planetName.equalsIgnoreCase("Titan") && secretCode == 1232){
                current = planetName;
                Moon Titan = new Moon(planetName, secretCode);
                theStack.push(Titan);
            } else if (planetName.equalsIgnoreCase("Europa") && secretCode == 9007){
                current = planetName;
                Moon Europa = new Moon(planetName, secretCode);
                theStack.push(Europa);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Europa")){

            if (planetName.equalsIgnoreCase("Enterprise") && secretCode == 1212){
                current = planetName;
                Moon Enterprise = new Moon(planetName, secretCode);
                theStack.push(Enterprise);
            } else if (planetName.equalsIgnoreCase("Rhea") && secretCode == 5623){
                current = planetName;
                Moon Rhea = new Moon(planetName, secretCode);
                theStack.push(Rhea);
            } else if (planetName.equalsIgnoreCase("Metis") && secretCode == 2535){
                current = planetName;
                Moon Metis = new Moon(planetName, secretCode);
                theStack.push(Metis);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Elara")){

            if (planetName.equalsIgnoreCase("Titan") && secretCode == 1232){
                current = planetName;
                Moon Titan = new Moon(planetName, secretCode);
                theStack.push(Titan);
            } else if (planetName.equalsIgnoreCase("Metis") && secretCode == 2535){
                current = planetName;
                Moon Metis = new Moon(planetName, secretCode);
                theStack.push(Metis);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Metis")){

            if (planetName.equalsIgnoreCase("Europa") && secretCode == 9007){
                current = planetName;
                Moon Europa = new Moon(planetName, secretCode);
                theStack.push(Europa);
            } else if (planetName.equalsIgnoreCase("Elara") && secretCode == 1264){
                current = planetName;
                Moon Elara = new Moon(planetName, secretCode);
                theStack.push(Elara);
            } else if (planetName.equalsIgnoreCase("Io") && secretCode == 4792){
                current = planetName;
                Moon Io = new Moon(planetName, secretCode);
                theStack.push(Io);
                System.out.println("You have acquired the Macho Orb! Return to the Enterprise.");
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else if (current.equals("Io")){

            if (planetName.equalsIgnoreCase("Metis") && secretCode == 2535){
                current = planetName;
                Moon Metis = new Moon(planetName, secretCode);
                theStack.push(Metis);
            } else {
                logOut = 0;
                System.out.println("You are dead.");
            }

        } else {
            logOut = 0;
            System.out.println("You are dead.");

        }

    } while((!current.equals("Io")) || (logOut != 0));


}

The main thing that is wrong here is that the do-while loop will not exit. The premise of the program is to jump from planet to planet while the planetName is the destination and the current is the current planet you are on. What I want is the loop to exit when current is "Io". The destination becomes the current planet after each "jump" to another planet. I have tested it, and current does equal "Io" when it is supposed to at the

    if (planetName.equalsIgnoreCase("Io") && secretCode == 4792) {
        current = planetName;
        Moon Io = new Moon(planetName, secretCode);
        theStack.push(Io);
        System.out.println("You have acquired the Macho Orb! Return to the Enterprise.");
    }

However, the loop keeps going.

6
  • Change the || in the exit condition to &&, otherwise they have to log out AND make it to Io in order to end the game. Commented Apr 20, 2015 at 21:23
  • Wow, I can't believe it was such a little error. Thank you so much Commented Apr 20, 2015 at 21:28
  • and I suggest you to try refactoring, using these concepts: polymorphism and factory. Using a hashtable could be useful as well. Commented Apr 20, 2015 at 21:38
  • @onof He clearly said that he is a beginner, I have had two years of learning Java in school, and I dont know what a hashmap is. I would suggest that you keep in mind an OP's skill when recommending other things to use in code. At least provide a link to documentation Commented Apr 20, 2015 at 21:41
  • @JohnnyCoder I'm sorry if my comment sounds rude to you, it wasn't my intention to hurt anyone. I just suggested the OP how to improve its code. Anyway, just googling polymorphism and hashtable a lot of resources can be found about them. Commented Apr 20, 2015 at 21:48

1 Answer 1

2

while((!current.equals("Io")) || (logOut != 0));

should be changed to

while((!current.equals("Io")) && (logOut != 0));

Sign up to request clarification or add additional context in comments.

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.