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 2 of 2

Thread: Bad Output

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

    Post Bad Output

    I was writing up a little game kit, when i ran into a little problem. When you enter a command, it skips past all the if/else statements, and defaults to the final else statement.

    import java.util.*; //used to make imput work
     
    class Text_Game_Kit
    {
    	//main method, where the game takes place
    	public static void main(String args[])
    	{
    		//Declaring everything
     
     
     
    		//position as a Y cordinate
    		int POSY = 0;
     
    		//position as an X cordinate
    		int POSX = 0;
     
    		//Placeholder for input
    		String input = "";
     
    		//The First Chest is not open
    		boolean Chest1 = false;
    		String Chest1OPCL = " closed"; // the first chest is closed. This is to enhance the text.
     
    		//Is there an enemy?
    		boolean enemy = false;
    		String enemyname = ""; //Type of enemy. i.e. Skeleton, Zombie, etc.
     
    		// boolean to see if the game is done
    		boolean done = false;
     
    		//if the game is not done, keep running
    		while (!done)
    		{
     
    			//quickly check if the game is done. if it is, break everything
    			if (done)
    			{
    				break;
    			}
     
     
    			//these are commands. try to see how they work. all commands must be lowercase.
     
    			//Movement Outputs: tells the user what the character sees
     
    			if (POSX==0&&POSY==0) //If you are at the starting position: 0,0. You see a chest from here. Reach it by saying "move up" 3 times.
    			{
    				System.out.println("You wake up in a poorly lit room. You see a" + Chest1OPCL + " chest 3 steps to the north");
    			}
    			else if (POSX==0&&POSY==3) //You have just moved up 3 times. you can open the chest by saing "open"
    			{
    				System.out.println("There is a" + Chest1OPCL + " chest in front of you.");
    			}
     
     
    			//Danger Commands: tells the user other information besides the Movement Outputs
    			if (enemy)
    			{
    				System.out.println("You are being attacked by a" + enemyname + ".");
    			}
     
     
    			//Get the input
    			input=in();
     
     
    			//handle the input. This includs all do able commands. typing help will list them. typing help and then a command will describe it. The only changing here should be to add commands
     
    			//Moving Commands
    			if (input==("up")) 
    			{
    				POSY++; //move up
    			}
    			else if (input==("down"))
    			{
    				POSY--; //go down
    			}
    			else if (input==("left"))
    			{
    				POSX--; //move left
    			}
    			else if (input==("right"))
    			{
    				POSX++; //move right
    			}
     
    			//opening commands: deals with: is there a chest, is it open, and what happens
     
    			else if (input==("open"))
    			{
    				if (POSX==0&&POSY==3&&!Chest1) //if the chest isnt open and you are at 0,3: the place where chest1 is.
    				{
    					System.out.println("You open the chest and find a sword. You hear the rattling of bones behind you.");
    					enemy=true;
    					enemyname=" Skeleton";
    					Chest1=true;
    					Chest1OPCL="n open"; //now text thinks its open
    					// you open the chest and are now being attacked by a skeleton. use "attack" to kill it.
    				}
     
    				// you can insert more chests here, you must use else if
     
    				//if there is no chest
    				else
    				{
    					System.out.println("You see nothing to open!");
    				}
     
    			}
     
    			//Attacking Command
    			else if (input=="attack")
    			{
    				if (enemy)
    				{
    					System.out.println("You attack a" + enemyname + ". It falls rather easily.");
     
    					//Staging. this is rather difficult because the attack command dosent know what you just attacked.
    					if (enemyname == " Skeleton")
    					{
    						done=true; //in this short kit, killing the skeleton is the end. however, you can use anything here. if you want, you can make this progress, or have another enemy pop up, a door open, etc. if you need help here, just ask. I understand its a bit vague.
    					}
    				}
    				else
    				{
    					System.out.println("You find nothing to attack, so you swing at the air for good measure");
    				}
    			}
     
    			//help, and any variants. Add more if you make new commands
    			else if (input=="help")
    			{
    				System.out.println("Commands are: up, down, left, right, open, help, and attack. Say help with a command after it to see more information"); //make sure to put any added here.
    			}
     
     
    			//indepth command help
    			else if (input=="help up")
    			{
    				System.out.println("This makes you move fowdard");
    			}
    			else if (input=="help down")
    			{
    				System.out.println("This makes you move backwards");
    			}
    			else if (input=="help left")
    			{
    				System.out.println("This makes you move left");
    			}
    			else if (input=="help right")
    			{
    				System.out.println("This makes you move right.");
    			}
    			else if (input=="help open")
    			{
    				System.out.println("This opens a chest, if there is one.");
    			}
    			else if (input=="help attack")
    			{
    				System.out.println("This attacks an enemy, if there is one.");
    			}
    			else if (input=="help help")
    			{
    				System.out.println("You IQ must me in the double digits");
    			}
     
    			//The end of the command cycle. all other commands must be above this
    			else
    			{
    				System.out.println("That is not a command. Try using help to find more commands.");
    			}
     
     
     
    		}
     
    		//end of the loop. only goes here when the game is done
    		System.out.println("Congradulations! You've won!");
     
    	}
     
     
    	//This method gets input
    	public static String in()
    	{
    		Scanner scan=new Scanner(System.in);
    		String word = "Temp";
    		word=scan.next();
    		return word;
    	}
    }


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Bad Output

    You know that strings needs to be compared using the "equals(...)" method instead of the regular "==" expression?

Similar Threads

  1. I need help, bad.
    By Mickyy in forum Java Theory & Questions
    Replies: 2
    Last Post: October 24th, 2013, 08:40 AM
  2. Is this bad code design?
    By JavaGirl9001 in forum Exceptions
    Replies: 1
    Last Post: November 28th, 2011, 03:25 AM
  3. Is my book bad or am I doing 'wrong'?
    By Catgroove in forum Java Theory & Questions
    Replies: 6
    Last Post: November 16th, 2010, 02:51 PM
  4. Not Looping? (do - while) bad execution!
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 23rd, 2009, 08:51 PM
  5. Hi, I'm new here. Need help bad!
    By jpechart in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 16th, 2009, 03:03 AM