# Guessing game help

• March 22nd, 2013, 12:19 AM
dx2731
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:
Attachment 1917

and this is the code I have to modify to include the new animals in:
Code java:

```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[-O<
• March 22nd, 2013, 05:25 AM
Norm
Re: Guessing game help
[code=java]
[/code]
to get highlighting and preserve formatting.

Quote:

more than four animals.
do you know about arrays or arraylists? They would allow you to have many animals.
• March 22nd, 2013, 07:52 AM
Chris.Brown.SPE
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.
• March 22nd, 2013, 11:01 PM
dx2731
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 :(
• March 23rd, 2013, 04:31 AM
dx2731
Re: Guessing game help