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

Thread: Guessing game help

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Guessing game help

    Been made to do this task where I need to make an animal guessing game using if-else. I cannot figure out how to make this code so it will add more than four animals. The game is basically

    Think of an animal.
    Is it a bird? yes
    Can it fly? no
    Is it an emu? no
    Oh. Well, thank you for playing.

    This is all the animals I need to include:
    TREE.JPG

    and this is the code I have to modify to include the new animals in:
    public class animalquiz
    {
     public static void main(String[] args)
        {
             public static void main(String[] args)
        {
            Scanner keyboard = new Scanner(System.in);
            boolean answerIsCorrect;
     
            System.out.println("Think of an animal.\n");
     
            if(ask("Is it a bird? ", keyboard))
            {
                if(ask("Can it fly? ", keyboard))
                {
                    answerIsCorrect = ask("Is it a kookaburra? ", keyboard);
                }
                else
                {
                    answerIsCorrect = ask("Is it an emu? ", keyboard);
                }
            }
            else
            {
                if(ask("Does it lay eggs? ", keyboard))
                {
                    answerIsCorrect = ask("Is it a platypus? ", keyboard);
                }
                else
                {
                    answerIsCorrect = ask("Is it a kangaroo? ", keyboard);
                }
            }
     
     
            if(answerIsCorrect)
            {
                System.out.println("Yes! I am invincible!");
            }
            else
            {
                System.out.println("Oh. Well, thank you for playing.");
            }
        }
     
        /**
         * A utility method to ask a yes/no question
         * 
         * @param question the question to ask
         * @param a scanner for user input
         * 
         * @return whether the user answered "yes" (actually, whether the user answered anything starting with Y or y)
         */
        private static boolean ask(String question, Scanner keyboard)
        {
            System.out.print(question);
     
            String answer = keyboard.nextLine().trim();
     
            return answer.charAt(0) == 'Y' || answer.charAt(0) == 'y';
        }
    }

    I hope someone can help and explain how I can do this


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Guessing game help

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    more than four animals.
    do you know about arrays or arraylists? They would allow you to have many animals.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Guessing game help

    This looks pretty simple in my opinion. Every question is an if statement and every end condition with an animal is an animal guess. For your if statements, the yes branch is your true and the no branch is your else. The example you have has way more than enough information for you to make this happen. I would start by drawing a picture of what is currently there. Maybe that would help you correlate the similarities between the picture and the if statement tree you are trying to build.

    Norm, unless i'm missing something i don't see why he would need an array. The level of complication in this assignment isn't anywhere close to that. They just want to figure out if he can copy paste an if statement and keep track of the logic.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Guessing game help

    Please could you give a sample of one of my paths? because i have tried everything and when i test it, it just doesnt work. especially because i cant get past three questions and three animals

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Guessing game help

    please help im desperate

    and also i have two public static void main(String[] args) in my code sorry.

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Guessing game help

    I got the program, will be asking for help in whats wrong with my code, thankyou for the help

Similar Threads

  1. Guessing Game
    By Spark2.0 in forum Object Oriented Programming
    Replies: 4
    Last Post: June 6th, 2012, 12:14
  2. Guessing game help
    By np657 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 12th, 2012, 07:39
  3. guessing game
    By scottey313 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2011, 13:30
  4. Guessing Game
    By scottey93 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 7th, 2011, 13:50
  5. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 11:41