【问题标题】:JSP - Get score from a jsp quiz applicationJSP - 从 jsp 测验应用程序中获取分数
【发布时间】:2012-05-07 15:05:57
【问题描述】:

您好,我正在 jsp 中创建一个测验应用程序,并为每个问题使用不同的 jsp 页面。我想在回答每个问题后记分。我的问题是我正在从数据库中选择随机答案,其中也包含正确的答案。由于我无法猜测正确答案会出现在哪个字母上,你能建议我应该怎么做吗?我发布第一个和第二个问题 jsp 是为了让您了解我是如何做到的:

q1.jsp

<%@page import="java.util.Random"%>
<%@page import="java.util.ArrayList"%>
<%@page import="org.me.jsp.beans.WordBean"%>
<%@page import="java.util.List"%>
<!--This JSP acts as the first question in a multiple choice quiz, with the user
asked to submit their answer to the question-->

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id = "wordData" scope = "request"
             class = "org.me.jsp.beans.WordDataBean" />
<html>
    <head>
        <title>Big Java Quiz, question 1</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <%if (request.getParameter("choice").equals("N")) {
                out.print("<meta http-equiv='refresh' content='0;url=options.jsp'/>");
            }
            //Redirects user back to index if they did not want to take quiz%>
        <form action="q2.jsp" method="POST">
            <%
                List<WordBean> wordList = wordData.getWordList();
                List<String> answersList = new ArrayList<String>();
                Random random = new Random();
                Random forAnswers = new Random();

                WordBean goodOne = wordList.get(random.nextInt(wordList.size()));
                //take it out from the list
                wordList.remove(goodOne);
                //add it to the answers list
                answersList.add(goodOne.getGermanName());
                WordBean fakeOne = wordList.get(random.nextInt(wordList.size()));
                //take it out from the list
                wordList.remove(fakeOne);
                //add it to the answers list
                answersList.add(fakeOne.getGermanName());
                WordBean fakeTwo = wordList.get(random.nextInt(wordList.size()));
                //take it out from the list
                wordList.remove(fakeTwo);
                //add it to the answers list
                answersList.add(fakeTwo.getGermanName());

            %>What is the English word for the German word <%=goodOne.getEnglishName()%>?<br>

                <%
                    char letter = 'A';
                    for (String answer : answersList) {
                %>
                <input type="radio" name="q1Answer" value=""/><label for="<%=letter%>"><%=letter%>)<%=answersList.get(forAnswers.nextInt(3))%>  /> 
                    <% //point to the next letter
                            letter++;
                        }
                        %>
                    <input type="submit" value="Submit"/>
                    </form>
                    </body>
                    </html>

q2.jsp

<%-- 
    Document   : q2
    Created on : 06-May-2012, 18:54:32
    Author     : encore
--%>

<!--This JSP acts as the second question in a multiple choice quiz, with the user
asked to submit their answer to the question-->

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Big Java Quiz, question 2</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <%int score = 0;
        if(request.getParameter("q1Answer").equals("C"))
            score++; //Increments score if answer submitted was correct%>
        <form action="q3.jsp" method="POST">
            Your current score is: <%out.print(score);%>/20
            <input type="hidden" name="q2Score" value="<%out.print(score);%>"/>
            <!--Hidden button allows score to be accessed by next JSP-->
            <b>Question 2.</b> When an exception is generated it is said to have been _________?<br/><br/>
            <input type="radio" name="q2Answer" value="A"/><label for="A">A) Built</label><br/>
            <input type="radio" name="q2Answer" value="B"/><label for="B">B) Thrown</label><br/>
            <input type="radio" name="q2Answer" value="C"/><label for="C">C) Caught</label><br/>
            <input type="radio" name="q2Answer" value="D"/><label for="D">D) Detected</label><br/><br/>
            <input type="submit" value="Submit"/>
        </form>
    </body>
</html>

注意:第二题的形式显然会改成别的形式,目前我正在集中精力拿分数。

【问题讨论】:

  • 我不明白你在问什么。如果是随机测验,并且测验是多项选择题,那么您将从数据库中随机选择 questions。当您检索随机问题时,您还将检索所有答案,包括正确的答案。因此,您应该拥有做出任何决定所需的所有信息。
  • 不不不,问题是固定的,将是“德语单词的英文单词是什么[从数据库中获取单词]?”然后我用从同一个数据库中提取的单词生成三个随机答案,这也是问题的相应答案。 ;)
  • 我不知道正确答案会出现在三个可能的字母中的哪一个。我需要一个命令来获取与问题的相应答案相同的命令
  • 所以我需要的不是request.getParameter("q1Answer").equals("C"),而是可以替换“C”
  • 一般来说,当您对测验进行评分时,您会从数据库中查找正确答案,并将其与用户给出的答案进行比较。据推测,当他们选择 A、B、C 或 D 时,您知道哪个单词对应于他们选择的字母。你也知道哪个词是正确的。如果他们选择的单词等于正确的单词,那么他们的答案是正确的。

标签: java database jsp get


【解决方案1】:

在 Q1 的页面中,您应该知道正确答案,将其放入会话中,然后在下一页中检索。

【讨论】:

    猜你喜欢
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2012-09-30
    • 2016-02-01
    相关资源
    最近更新 更多