简体   繁体   中英

How can implement a question answer logic in front end as well as back end

In my website I am gonna show a main question with some answers.

So if the user clicks one of the answers I am gonna show some list of questions again with respective answers.So each answers has got certain questions again .This process goes on till there is a answer which has got no questions. How I can i do it most efficiently on the server side in java as well as on the html side . How can i store this structure in some model.

If anybody knows any kind of opensource project which implement this logic please help.

It sounds to me like you would need a tree:

  • A question would be represented as a node
  • An answer is an edge leading from one question to another
  • An answer not leading to a new question, would simply have the destination node as null.

In Java you would could represent it like this:

class Question {

    // The question, for example "What is the color of the sky?"
    String question;

    // List of answer alternatives: For example
    //     - Blue (with destination, "What is the color of the sun?")
    //     - Red  (with destination null)
    List<Answer> answers;

}


class Answer {

    // The answer, for example "Blue"
    String answer;

    // The next question (or null, if this answer is terminating)
    Question destinationQuestion;
}

To create a full tree of these objects, you need to either

  • Provide constructors that takes one argument per attribute

    In such case you need to build the tree from the leaves up (since you can't provide intermediate nodes with their arguments unless you've already created them)

  • Create "setter"-methods, create all questions and "set" the appropriate children where they are supposed to be. (This would allow you to build the tree from root to leaves.)


Other alternatives:

  • You could use a DAG (directed acyclic graph) if you would like to reuse parts of the tree in several places (ie, if several answers leads to the same answer)

  • You could use an ordinary directed graph if you would like to allow for cycles in the question system.

In either way the two classes I described above fits the bill!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM