Java help for a beginner - making a quiz

Suspended
Joined
12 Aug 2006
Posts
1,373
Location
CandyMountain
I am trying to make a quiz in java to help people to decide what classes will suit them in Uni.

On paper, it will look like a tree diagram, so that dependent on a person's yes/no answer to a question it will lead them down a different branch leading to an eventual conclusion.

My problem is this (I'm sure it's probably quite a beginner question, though it's currently troubling me a great deal!):

I'm having trouble calling the if/else methods purely on the basis of the yes/no answer. I can get it like a questionnaire so that it just runs through all the questions chronologically, collecting the answers, but cannot get it to call different methods depending on the answer given by the user.

Does that make sense?

Thanks for any help in advance!
 
Im not exactly sure what your getting at:

Code:
if (something)
  call method1()
else if (something else)
  call method2()
else if (something else2)
  if (something)
       do something
  else
       do something else
else
       do this

Or you could just use a switch... I guess your just getting at nested if statement? Would be better/easier if you gave an example of an input and what the different results of that could be because the way you write it atm is a little confusing.
 
Last edited:
I'm sorry if it was a little confusing. Yes, the code you posted is similar to what I've got down right now.
Questions along the lines of..
1. "Do you like a challenge? (answer yes/no)" --(if "yes" then call q.2, if "no" call q.3)
2. "Do you like scientific subjects? (answer yes/no)"
--(if "yes" then call q.4, if "no" call q.5)

Snippet below (please excuse any things you may consider basic/n00b in the following)
public String getRightForYou(String right){
String r = "";

if (right.equals("yes"))
r = "goto next method";

else if (right.equals("no"))
r = "goto other method";

else
r = "Please enter yes or no (in lowercase letters).";

etc.


I'm just not sure how to use a nested if statement in this circumstance.
 
Just roughly hacked something together, this can be improved on slightly since I duplicated a bit of code but you get the idea.

Code:
import java.util.Scanner;

public class Questionaire
{
    private Scanner sc = new Scanner(System.in);

    public void question1()
    {
        System.out.println("Do you like a challenge?");
        String input = sc.next();
        if (input.equalsIgnoreCase("yes"))
        {
            this.question2();
            return;
        }
        else if (input.equalsIgnoreCase("no"))
        {
            this.question3();
            return;
        }
        System.out.println("Please enter either yes or no");
        question1();
    }

    public void question2()
    {
        System.out.println("Do you like science subjects?");
        String input = sc.next();
        if (input.equalsIgnoreCase("yes"))
        {
            this.question4();
            return;
        }
        else if (input.equalsIgnoreCase("no"))
        {
            this.question5();
            return;
        }
        System.out.println("Please enter either yes or no");
        question2();
    }

    public void question3()
    {
    }

    public void question4()
    {
    }

    public void question5()
    {
    }

    public static void main(String[] args)
    {
        Questionaire q = new Questionaire();
        q.question1();
    }
}
 
Last edited:
I would write the list of questions as a bunch of functions then have some switch function also. So for example, to begin with ask question 1, then if the answer is yes, input 1 into the switch function and if no then input 2. Then question 2 would give 3 for yes, 4 for no etc. These values when passed into the switch function ( for example:
Code:
public void question1()
{
   string answer;
   //ask the question
   answer = given answer;

   if (answer == "yes")
   {
      questionSelector(1);
   }

   else if (answer == "no")
   {
      questionSelector(2);
   }
}

question2()
{
}

question3()
{
}

//etc

public static void questionSelector(int i){
 switch (i) {
case 1: question2(); break; //if question 1 was answered yes
case 2: question3(); break; //if question 2 was answered no
case 3: question4(); break; //if question 3 was answered yes
case 4: question5(); break; //if question 4 was answered no
default: System.println("invalid i value passed"); break;
//etc
}

I think thats what your trying to do right? syntax is prob wrong but hope you understand what i mean :)

damn beaten with a reply :P oh well you have 2 options now.
 
Hi,

Here's another way of doing it. Let me point out now I'm not trying to be smart, just thought doing it this way could help simplify things, and would enable you to build larger question trees with no more complexity. Would also introduce Java objects etc.

If you haven't got this far with Java perhaps put this away for later if you like.... The last thing I want to do is confuse.

Another way you could do this and simplify the conditions would be to define a class that models a question. The class would have four attributes:

questionid
questiontext
yesresponseid
noresponseid

Each question has a unique ID and piece of text. Each question also has a yes and no responseid which are the id of the question to display next for each response. Using this arrangement you can describe a tree-like structure by having an ArrayList containing one of these objects per question.

Display a question by finding it by it's ID in the array and then calling a method on the object to display it. If the user then answers Yes get the contents of the yesreponseid attribute of the current question and find the question object with this questionid in the array. Repeat the whole display/answer procedure. The end of a chain would be indicated by the appropriate responseid being set to 0.

Psuedo-code would be something like:

Code:
// Set up questarray to contain multiple question objects.

// Start at the first question.
question curquestion = questarray[1];

// Repeat until we reach the end.
while (curquestion != null) {
    // Display the question.
    curquestion.display();

    // Get response
    String response = userinput

    // Find next question id.
    int nextid=0;

    if (response.compareTo("Y")==0) {
        nextid = curquestion.yesresponseid;
    } else {
        nextid = curquestion.noresponseid;
    }

    // Prepare for the next question.
    curquestion = null;

    // Find the question object with the questionid of nextid in the
    // questarray and set curquestion to this. If no object is found then
    // we've got to the end of the question sequence. Curquestion being set to
    // null will end the loop.
}

Hope that's some help or at least interesting. If you want any more detail please let me know and I'll try to help.

Jim
 
With much thanks to Una, I managed to construct the simple quiz (posted below). But it seems now we have to integrate radio buttons (??). Have been playing around with some bits and pieces I have found online but am unable to intergrate them properly. Any hints or tips?

Code:
import java.util.Scanner;

public class Questionaire
{
    private Scanner a = new Scanner(System.in);

    public void question1()
    {
        
        
        System.out.println("Do you like a challenge?");
        String input = a.next();
        if (input.equals("yes"))
        {
            this.question2();
            return;
        }
        else if (input.equals("no"))
        {
            this.question3();
            return;
        }
        System.out.println("Please enter either yes or no");
        question1();
    }

    public void question2()
    {
        System.out.println("Do you enjoy taking a problem and figuring it out logically?");
        String input = a.next();
        if (input.equals("yes"))
        {
            this.question4();
            return;
        }
        else if (input.equals("no"))
        {
            this.question5();
            return;
        }
        System.out.println("Please enter either yes or no");
        question2();
    }

    public void question3()
    {
        System.out.println("If you don't like a challenge, college may not be for you. If you are currently ");
        System.out.println("taking a computer science class, you may want to consider dropping it. ");
    }

    public void question4()
    {
        {
        System.out.println("Are you a creative thinker?");
        String input = a.next();
        if (input.equals("yes"))
        {
            this.question6();
            return;
        }
        else if (input.equals("no"))
        {
            this.question8();
            return;
        }
        System.out.println("Please enter either yes or no");
        question2();
    }
    }

    public void question5()
    {
        {
        System.out.println("Do you enjoy working with technology? ");
        String input = a.next();
        if (input.equals("yes"))
        {
            this.question8();
            return;
        }
        else if (input.equals("no"))
        {
            this.question9();
            return;
        }
        System.out.println("Please enter either yes or no");
        question2();
    }
    }
    public void question6()
    {
        {
        System.out.println("Do you enjoy working with technology? ");
        String input = a.next();
        if (input.equals("yes"))
        {
            this.question7();
            return;
        }
        else if (input.equals("no"))
        {
            this.question9();
            return;
        }
        System.out.println("Please enter either yes or no");
        question2();
    }
    }

    public void question7()
    {
      System.out.println("It sounds like you have all the qualities which would be great for computer science and you would really enjoy Intro to Computer Science! ");
      System.out.println("You may even want to consider a major in computer science!");
    }
    public void question8()
    {
      System.out.println("It sounds like you have some qualities which maybe great for computer science. ");
      System.out.println("I would suggest taking Intro to Computer Science and see if it suits you.");
      System.out.println("You may want to consider a minor in computer science!");
    }
    public void question9()
    {
      System.out.println("Unfortunately it doesn't look like computer science will suit you.");
      System.out.println("Enjoying working with technology is a must because you will be working with computers all the time!");
      

    }

    public static void main(String[] args)
    {
        Questionaire q = new Questionaire();
        q.question1();     
    }
}
 
JIMA said:
Hi,

Here's another way of doing it. Let me point out now I'm not trying to be smart, just thought doing it this way could help simplify things, and would enable you to build larger question trees with no more complexity. Would also introduce Java objects etc.

If you haven't got this far with Java perhaps put this away for later if you like.... The last thing I want to do is confuse.

Another way you could do this and simplify the conditions would be to define a class that models a question. The class would have four attributes:

questionid
questiontext
yesresponseid
noresponseid

Each question has a unique ID and piece of text. Each question also has a yes and no responseid which are the id of the question to display next for each response. Using this arrangement you can describe a tree-like structure by having an ArrayList containing one of these objects per question.

Display a question by finding it by it's ID in the array and then calling a method on the object to display it. If the user then answers Yes get the contents of the yesreponseid attribute of the current question and find the question object with this questionid in the array. Repeat the whole display/answer procedure. The end of a chain would be indicated by the appropriate responseid being set to 0.

Psuedo-code would be something like:

Code:
// Set up questarray to contain multiple question objects.

// Start at the first question.
question curquestion = questarray[1];

// Repeat until we reach the end.
while (curquestion != null) {
    // Display the question.
    curquestion.display();

    // Get response
    String response = userinput

    // Find next question id.
    int nextid=0;

    if (response.compareTo("Y")==0) {
        nextid = curquestion.yesresponseid;
    } else {
        nextid = curquestion.noresponseid;
    }

    // Prepare for the next question.
    curquestion = null;

    // Find the question object with the questionid of nextid in the
    // questarray and set curquestion to this. If no object is found then
    // we've got to the end of the question sequence. Curquestion being set to
    // null will end the loop.
}

Hope that's some help or at least interesting. If you want any more detail please let me know and I'll try to help.

Jim


You could even go further and store the questions in an xml document, making the program usable for *any* question set.....

i.e
Code:
<question id="1" text="What is your favourite colour">
   <answer value="blue" gotoquestion="2"/>
   <answer value="green" gotoquestion="3"/>
   <answer value="red" gotoquestion="4"/>
   <answer value="yellow" gotoquestion="5"/>
   <answer value="orange" gotoquestion="6"/>
</question>
<question id="2" text="What shade of blue?">
   <answer value=" light blue" gotoquestion="2"/>
   <answer value=" dark blue" gotoquestion="2"/>
</question>
 
crystaline said:
But it seems now we have to integrate radio buttons (??). Have been playing around with some bits and pieces I have found online but am unable to intergrate them properly. Any hints or tips?

Have you read this, http://java.sun.com/docs/books/tutorial/uiswing/components/button.html#radiobutton

Turning this into a GUI application is quite different. I guess what you need to do is draw radio buttons for say question 1, then depending on selection update and draw either question 2 or 3 for example. You can fire an event once a radio button is clicked, then update the GUI to reflect the new question. You could also have a submit button that way the user makes there selection and then clicks submit, and the GUI will update with the new question.

I tend to use MVC/Observer pattern for my GUI applications but I guess thats a bit of an overkill for this.
 
Last edited:
Una, thanks for the idea of using XML to hold the questions. It would certainly be very flexible, getting away from just a yes/no answer.

It would also make it easier to generate the question's display directly from the XML document (using XSLT to create HTML in the case of a web-based app).

Thanks,
Jim
 
Back
Top Bottom