Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Type Adventure

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Type Adventure

    I am trying to use a loop and if/else statements along with my Switch to create a very simple type adventure.
    I want there to be a boolean to add my quit function, but am wondering if a Sentinel-controlled loop would be better. I simply need the player to be able to travel from room to room, an error message come up if they press the wrong button, allowing them to try again, and then a quit function available at all times. I know how to add a quit function to each input section but would have been trying to use a boolean and while loop to solve this, to no avail.

    Here is my current code (not finished but my only trouble thus far is with the quit function)

    it compiles and runs but only follows through with the quit function in the first step and when it reaches case 2.(Dinner Hall) I cannot get it to enter into the next room.

    Thanks:

    import java.util.Scanner;

    public class Boolean2
    {
    public static void main(String[] args)
    {
    int room;
    String command=null;
    String command2=null;
    boolean quit;

    room=1;
    System.out.println("Press s to start!");

    {Scanner keyboard= new Scanner(System.in);
    command=keyboard.nextLine();
    if (command.equals("q"))
    {quit=true;
    System.out.println("Game Over");
    System.exit(0);}
    else
    {System.out.println("Let's Begin The Adventure!!");}


    quit=false;
    while (quit==false)
    {
    switch(room){
    case 1:
    System.out.println("You are in the Kitchen,you may go N");
    break;
    case 2:
    System.out.println("You are in the Dinner Hall,you may go S or E");
    break;
    case 3:
    System.out.println("You are in the Master Bedroom,you may go E or W");
    break;
    case 4:
    System.out.println("You are now a victor!!");
    System.out.println("Game Over");
    quit=true;
    break;

    default:
    System.out.println("A error has occured");
    quit=true;
    break;
    }

    if (quit==false)

    command=keyboard.nextLine();
    if (command.equals("N"))
    {room=2;
    System.out.println("Welcome to room 2");}

    else
    {System.out.println("Error!!!");}



    }
    command2=keyboard.nextLine();
    if (command2.equals("E"))
    {room=3;
    System.out.println("Welcome to room 3");}
    else
    {System.out.println("Error!!");}




    }
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Type Adventure

    When posting code, please use the highlight tags to preserve formatting.

    What does this code do? Does it compile? Does it run? What behavior is it exhibiting that is incorrect?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Type Adventure

    Thanks for the heads up!
    I edited it!

Similar Threads

  1. Text adventure game help
    By Death_The_Kid in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 26th, 2013, 05:45 AM
  2. About data type
    By zahidhasan in forum Java Theory & Questions
    Replies: 1
    Last Post: March 31st, 2013, 10:37 AM
  3. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  4. Not sure what type of variable to use.
    By mjpam in forum Java Theory & Questions
    Replies: 13
    Last Post: July 13th, 2010, 08:58 PM
  5. cannot be resolved to a type
    By Teraphim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 16th, 2010, 09:42 AM

Tags for this Thread