如何在前端和后端实现问答逻辑

发布于 2024-12-01 18:36:11 字数 199 浏览 1 评论 0原文

在我的网站上,我将展示一个主要问题和一些答案。

因此,如果用户单击其中一个答案,我将再次显示一些问题列表以及相应的答案。因此每个答案都会再次出现某些问题。这个过程一直持续到出现没有问题的答案为止。我怎样才能在java的服务器端以及html端最有效地做到这一点。我如何将这个结构存储在某个模型中。

如果有人知道实现此逻辑的任何类型的开源项目,请提供帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泼猴你往哪里跑 2024-12-08 18:36:12

在我看来,您需要一棵树:

  • 一个问题将表示为一个节点,
  • 一个答案是从一个问题通向另一个问题的一条边,
  • 一个答案不会导致新问题,只是将目标节点设置为空。

在 Java 中,您可以这样表示:

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;
}

要创建这些对象的完整树,您需要

  • 提供每个属性采用一个参数的构造函数

    在这种情况下,您需要从叶子向上构建树(因为除非您已经创建了中间节点,否则您无法提供中间节点及其参数)

  • 创建“setter”方法,创建所有问题并将适当的孩子“设置”在他们应该在的位置。 (这将允许您从根到叶构建树。)


其他替代方案:

  • 如果您想在多个位置重用树的部分内容(即,如果有多个答案),则可以使用 DAG(有向无环图)导致相同的答案)

  • 如果您愿意,可以使用普通有向图允许问题系统中存在循环。

无论哪种方式,我上面描述的两个类都符合要求!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文