Working on a simple text game "Choose your adventure"

I am new to Java and coding in general. I started working on these tutorials with great success until I got to this one . I know that in my code I have not yet included the "up" option because I wanted to make sure the "kitchen" worked correctly at first.

The code compiles just fine, however when I run it on the cmd line I can choose the first option to go to "kitchen" but when I choose to view "pantry" it takes 2 cmd lines with "pantry" to execute, actually learning this ...

Also, if I select the "run away" option after searching the "pantry", it will not print the text with the "run away" option.

Sorry if there are easier ways to do this, but I haven't looked into them yet.

Thanks for any help!

import java.util.Scanner;

public class Adventure1
{
public static void main( String[] args ){
    Scanner keyboard = new Scanner(System.in);

String Go, Look, Pantry, Eat;

System.out.println( " WELCOME TO MY TINY ADVENTURE");
System.out.println("  ");
System.out.println( " You are in a creepy house! Would you like to go 'upstairs' or into the 'kitchen'? ");
System.out.print( "> ");
Go = keyboard.next();

if (Go.equalsIgnoreCase("kitchen"))
{System.out.println("There is a long countertop with dirty dishes everywhere. Off to one side there is, as you'd expect, a refrigerator. You may open the 'refrigerator' or look in the 'pantry'. ");}
System.out.print(">  ");
Look = keyboard.next();


    if (Look.equalsIgnoreCase( "refrigerator" ))
{System.out.println("Inside the refrigerator you see food and stuff. It looks pretty nasty. Would you like to eat some of the food, 'Yes' or 'No'?");}
System.out.print(">  ");
Eat = keyboard.next();

        if (Eat.equalsIgnoreCase("Yes"))
    {System.out.println("  ");
    System.out.println("You live!");}

        else if (Eat.equalsIgnoreCase("No"))
    {System.out.println("  ");
    System.out.println("You die of starvation!");}



else if (Look.equalsIgnoreCase( "pantry" ))
{System.out.println("There is a killer inside. Do you want to 'fight' them, or 'run away'?");}
System.out.print(">  ");
Pantry = keyboard.next();

        if (Pantry.equalsIgnoreCase("fight"))
    {System.out.println("  ");
    System.out.println("You're weak and die");}

        else if(Pantry.equalsIgnoreCase("run away"))
    {System.out.println("  ");
    System.out.println("You died because your too slow & can't run");}

}

    }

      

+3


source to share


2 answers


Look at the logic behind this section ...

if (Go.equalsIgnoreCase("kitchen")) {
    System.out.println("There is a long countertop with dirty dishes everywhere. Off to one side there is, as you'd expect, a refrigerator. You may open the 'refrigerator' or look in the 'pantry'. ");
}
System.out.print(">  ");
Look = keyboard.next();

if (Look.equalsIgnoreCase("refrigerator")) {
    System.out.println("Inside the refrigerator you see food and stuff. It looks pretty nasty. Would you like to eat some of the food, 'Yes' or 'No'?");
}
System.out.print(">  ");
Eat = keyboard.next();

      

If a user enters the "kitchen", you ask them for refrigerator

or pantry

if they enter pantry

, you go straight to blank prompts, you haven't actually handled the likelihood that the user might enter anything other thanrefrigerator

Your whole logical chain is broken, you don't divide sections into separate blocks of logic to handle the current scenario.

For example, something like ...



Scanner keyboard = new Scanner(System.in);

String Go, Look, Pantry, Eat;

System.out.println(" WELCOME TO MY TINY ADVENTURE");
System.out.println("  ");
System.out.println(" You are in a creepy house! Would you like to go 'upstairs' or into the 'kitchen'? ");
System.out.print("> ");
Go = keyboard.next();

if (Go.equalsIgnoreCase("kitchen")) {
    System.out.println("There is a long countertop with dirty dishes everywhere. Off to one side there is, as you'd expect, a refrigerator. You may open the 'refrigerator' or look in the 'pantry'. ");
    System.out.print(">  ");
    Look = keyboard.next();

    if (Look.equalsIgnoreCase("refrigerator")) {
        System.out.println("Inside the refrigerator you see food and stuff. It looks pretty nasty. Would you like to eat some of the food, 'Yes' or 'No'?");
        System.out.print(">  ");
        Eat = keyboard.next();

        if (Eat.equalsIgnoreCase("Yes")) {
            System.out.println("  ");
            System.out.println("You live!");
        } else if (Eat.equalsIgnoreCase("No")) {
            System.out.println("  ");
            System.out.println("You die of starvation!");
        }
    } else if (Look.equalsIgnoreCase("pantry")) {
        System.out.println("There is a killer inside. Do you want to 'fight' them, or 'run away'?");
        System.out.print(">  ");
        Pantry = keyboard.next();

        if (Pantry.equalsIgnoreCase("fight")) {
            System.out.println("  ");
            System.out.println("You're weak and die");
        } else if (Pantry.equalsIgnoreCase("run away")) {
            System.out.println("  ");
            System.out.println("You died because your too slow & can't run");
        }
    }
}

      

will group each logical block into its own group. This would lead you to be able to use methods to further isolate your logic.

The next problem you will have to face is what it takes when they don't enter what you expect

Another problem you will run into is this Scanner#next

will return the following work, so something like run away

it won't work. You can instead useScanner#nextLine

+1


source


Your logic has been disabled. See where the pantry actually was.



import java.util.Scanner;

public class Adventure1 {
    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    String Go, Look, Pantry, Eat;

    System.out.println(" WELCOME TO MY TINY ADVENTURE");
    System.out.println("  ");
    System.out
        .println(" You are in a creepy house! Would you like to go 'upstairs' or into the 'kitchen'? ");
    System.out.print("> ");
    Go = keyboard.next();

    if (Go.equalsIgnoreCase("kitchen")) {
        System.out
            .println("There is a long countertop with dirty dishes everywhere. Off to one side there is, as you'd expect, a refrigerator. You may open the 'refrigerator' or look in the 'pantry'. ");
    }
    System.out.print(">  ");
    Look = keyboard.next();

    if (Look.equalsIgnoreCase("refrigerator")) {
        System.out
            .println("Inside the refrigerator you see food and stuff. It looks pretty nasty. Would you like to eat some of the food, 'Yes' or 'No'?");
    } else if (Look.equalsIgnoreCase("pantry")) {
        System.out
            .println("There is a killer inside. Do you want to 'fight' them, or 'run away'?");
        Pantry = keyboard.next();

        if (Pantry.equalsIgnoreCase("fight")) {
        System.out.println("  ");
        System.out.println("You're weak and die");
        }

        else if (Pantry.equalsIgnoreCase("run away")) {
        System.out.println("  ");
        System.out
            .println("You died because your too slow & can't run");
        }
    }
    System.out.print(">  ");
    Eat = keyboard.next();

    if (Eat.equalsIgnoreCase("Yes")) {
        System.out.println("  ");
        System.out.println("You live!");
    }

    else if (Eat.equalsIgnoreCase("No")) {
        System.out.println("  ");
        System.out.println("You die of starvation!");
    }
    System.out.print(">  ");

    }

}

      

+1


source







All Articles