开发者

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

开发者 https://www.devze.com 2023-03-30 21:25 出处:网络
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

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 (i.e., 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号