Results 1 to 4 of 4
  1. #1
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Some code help for a newbie?

    Hello, I've been doing Java for a little over a week and I'm trying to write this game type thing where you can go through different doors and make different decisions based on your input. In my code so far I'm having a hard time figuring out how to branch out the different options. The game doesn't make much sense yet, but as you can see you have the option of choosing "Gate 1 or Gate 2." For example, if you choose Gate 1, how can I get it to either open the chest or not open it and then continue the game while having that go on a different path compared to choosing Gate 2? Do I continue to write "else if" statements inside each section of code? Thanks, I hope this won't be too complicated for a newbie. Please critique anything I wrote.

    Java Code:
    import java.util.Scanner;
    
    public class kiwi {
    	public static void main (String args[]){
    		System.out.println("Hello my name is Andrew, what is your name?");
    		Scanner userInput = new Scanner(System.in);
    		String name = userInput.nextLine();
    		System.out.printf("Welcome to the site %s.\n", name);
    		System.out.println("Would you like to open Gate 1 or Gate 2 (Answer with a number '1' or '2').");
    	
    		boolean stop = false;
    		
    		while (stop == false){
    			String input = userInput.nextLine();
    				
    		if (input.equals("1")){
    			System.out.println("You have opened Gate 1, there is a treasure chest.");
    			System.out.println("Open it? (y/n)");
    			input = userInput.nextLine();{
    			}	
    			
    			
    			}
    		else if (input.equals("2")){
    			System.out.println("You have opened Gate 2, there are two Doors, A & B.");
    			System.out.println("Which one would you like to open? (Answer with 'a' or 'b')");
    			
    			}
    			else{
    				System.out.println("Invalid input.");
    				System.out.println("Would you like to open Gate 1 or Gate 2 (Answer with a number '1' or '2').");
    			}
    		}	
    	}
    }

  2. #2
    knightwriter is offline Member
    Join Date
    Nov 2011
    Posts
    49
    Rep Power
    0

    Default Re: Some code help for a newbie?

    You could create a chest class and chess method, then make a constructor in the main class. After that within the chest class you could continue down different paths.

  3. #3
    atac57 is offline Member
    Join Date
    Jan 2012
    Posts
    24
    Rep Power
    0

    Default Re: Some code help for a newbie?

    Thank you! I'm having a problem though, when I purposely type an input that I want the program to respond to as "Invalid" (for example, typing "fnskdalj" instead of "y" or "n" for yes or no) it continues to loop forever. I'm using a while loop. This only seems to be happening in my other two classes, not the main one which only shows "Invalid..." once. I appreciate the help!


    Java Code:
    import java.util.Scanner;
    
    public class kiwi {
    	public static void main (String args[]){
    		
    		System.out.println("Hello my name is Andrew, what is your name?");
    		Scanner userInput = new Scanner(System.in);
    		String name = userInput.nextLine();
    
    		System.out.printf("Welcome to the game %s.\n", name);
    		System.out.println("Would you like to open Gate 1 or Gate 2 (Answer with a number '1' or '2').");		
    		
    		boolean stop = false;
    	
    		while (stop == false){
    
    		String input = userInput.nextLine();	
    		if (input.equals("1")){		
    			System.out.println("You have opened Gate 1."); 
    			gate1 gate1Object = new gate1();
    			gate1Object.chest();{
    			}						
    			}
    		else if (input.equals("2")){
    			System.out.println("You have opened Gate 2.");
    			gate2 gate2Object = new gate2 ();
    			gate2Object.doors();
    			
    			}
    		else{
    			System.out.println("Invalid input.");
    			System.out.println("Would you like to open Gate 1 or Gate 2 (Answer with a number '1' or '2').");
    			}
    		}	
    	}
    }

    Java Code:
    import java.util.Scanner;
    
    public class gate1 {
    	
    	public void chest (){
    	System.out.println("There is a treasure chest behind Gate 1. Open it? (y/n)");
    	Scanner userInput = new Scanner(System.in);
    	String input = userInput.nextLine();{
    		
    	
    	boolean stop = false;
    	while (stop == false){
    	
    			
    	if(input.equals("y")){
    		System.out.println("You opened the treasure chest and have found a key inside.");
    		System.out.println("There are two doors ahead of you, 'blue' and 'red.'");
    		System.out.println("Which one would you like to open?");
    		stop = true;
    	}
    	else if (input.equals("n")){
    		System.out.println("You have been killed by a firebreathing dragon.");
    		System.out.println("RIP");
    		stop = true;
    		break;
    	}
    	else{
    		System.out.println("Invalid input.");
    		System.out.println("There is a treasure chest behind Gate 1. Open it? (y/n)");
    	 }
        }	
       }
      }
     }
    Java Code:
    import java.util.Scanner;
    
    public class gate2 {
    	
    	public void doors (){
    		System.out.println("There are two doors behind Gate 2, 'a' and 'b'.");
    		System.out.println("Which one would you like to open?");
    		Scanner userInput = new Scanner (System.in);
    		String input = userInput.nextLine();{
    			
    			boolean stop = false;
    			while (stop == false){
    				
    				if(input.equals("a")){
    					System.out.println("You have opened door 'a' and have found a sword!");
    						
    			}
    			else if(input.equals("b")){
    					System.out.println("You have found a secret passage way");					
    			}
    			else{
    				System.out.println("Invalid input.");
    			}
    		 }	
    	  }
       }
    }

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Some code help for a newbie?

    On the equals("a") and equals("b") branches you should set stop to true. In the else branch you should read another line of input (if that's what you mean to do).

    The same applies to the y/n case - you aren't reading the input.

    -----

    while(foo==false) is better written as while(!foo), and classes should start with a capital letter.
    Last edited by pbrockway2; 01-03-2012 at 06:12 AM.

Similar Threads

  1. Newbie question about basic code - "Hello World"
    By Kimomaru in forum New To Java
    Replies: 3
    Last Post: 10-13-2011, 06:56 PM
  2. newbie needs help w/ code in android
    By mailboxo71 in forum Android
    Replies: 3
    Last Post: 01-21-2011, 06:49 AM
  3. Newbie need help w/ Java code :)
    By mailboxo71 in forum New To Java
    Replies: 5
    Last Post: 01-21-2011, 06:48 AM
  4. newbie
    By jejerome in forum Introductions
    Replies: 1
    Last Post: 09-22-2010, 02:20 AM
  5. [newbie] messed up code
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-31-2009, 04:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •