0

嗨,我正在 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>

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

4

1 回答 1

1

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

于 2012-05-07T16:00:32.520 回答