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!
 
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.
 
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();     
    }
}
 
Back
Top Bottom