1

I am very new to this and was wondering if anyone could help me. I want the code to be able to stop asking questions after the answer is on certain questions. I get it to work on the first ones like the parrot and emu but I can't seem to add others. Please advise me what I am doing wrong.

The code so far is:

import java.util.Scanner;

/**
* Guessing Game.
* 
* @author O 
* @version 99
*/
public class Quiz
{
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 parrot? ", keyboard);
        }
        else
        {
            answerIsCorrect = ask("Is it an emu? ", keyboard);
        }

    {
        if(ask("Does it live in the ocean? ", keyboard))
    {
        if(ask("Is it a whale? ", keyboard))
        {
            answerIsCorrect = ask("Answer 1? ", keyboard);
        }
        else
        {
            answerIsCorrect = ask("Answer 2? ", 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("I am good!");
    }
    else
    {
        System.out.println("Drats! I guess I don't know then!");
    }
 }

 /**
 * 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';
  }
   }
sgcharlie
  • 1,006
  • 1
  • 10
  • 25
Ozzy
  • 39
  • 1
  • 3
  • 4
    I don't know why this is getting down votes. Everyone needs to start somewhere and while this might not be a question as interesting or complex as branch prediction on unsorted vs sorted arrays, Ozzy needs some guidance and that's what SO is here for. – sgcharlie Aug 24 '13 at 13:30
  • 1
    @sgcharlie I agree, this question is valid, and Ozzy has demonstrated a minimal effort and provided code. – Oscar Aug 24 '13 at 13:37

1 Answers1

1

You seem have an extra pair of brackets in your code (after the initial if statement. You probably want to remove those. Also, you'll probably want to add an else if. So, something like:

if(ask("Is it a bird? ", keyboard))
{
    if(ask("Can it fly? ", keyboard))
    {
        answerIsCorrect = ask("Is it a parrot? ", keyboard);
    }
    else
    {
        answerIsCorrect = ask("Is it an emu? ", keyboard);
    }           
}
else if(ask("Does it live in the ocean? ", keyboard))
{
    if(ask("Is it a whale? ", keyboard))
    {
        answerIsCorrect = ask("Answer 1? ", keyboard);
    }
    else
    {
        answerIsCorrect = ask("Answer 2? ", keyboard);
    }
}
else
{
    if(ask("Does it lay eggs? ", keyboard))
    {
        answerIsCorrect = ask("Is it a platypus? ", keyboard);
    }
    else
    {
        answerIsCorrect = ask("Is it a kangaroo? ", keyboard);
    }
}

Hope that helps. Good luck with learning how to program.

sgcharlie
  • 1,006
  • 1
  • 10
  • 25
  • 1
    @Ozzy when you get this up and running you might want to look into using a tree structure for the questions instead of if-else if-else; its structure is ideal for this kind of thing. – Shane Haw Aug 24 '13 at 14:05
  • The flow is like a tree , but will Tree Data Structure help the selection(if else nesting) ??? any better solution other that nested if-else for such algorithms ? – Srinath Ganesh Aug 24 '13 at 15:13
  • 1
    I agree with Shane. A tree structure would be a better implementation, especially if you ever want to read in the questions from a data source (like a database or a file) instead of having hardcoded strings. If you want this to be a serious program, then you should definitely look into a recursive structures. However, I would recommend mastering the `if-else` (and probably `for` loops) first. – sgcharlie Aug 24 '13 at 17:19