2012-05-07 13 views
0

こんにちは私はjspでクイズのアプリケーションを作成していて、すべての質問ごとに異なる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> 

注:フォームを、私はあなたに私はそれをやっているかのアイデアを与えるために第一及び第二の質問JSPのを掲示しています明らかに第二の質問の何かが他のものに変更されます、現在私はスコアを得ることに集中しています。

+0

私はあなたが何を求めているのか分かりません。それがランダムなクイズで、クイズが複数の選択肢である場合は、データベースからランダム*の質問*を選択します。ランダムな質問を取得すると、正しい回答を含むすべての回答も取得されます。したがって、*決断を下すために必要なすべての情報が必要です。 –

+0

いいえいいえ、問題は解決され、「ドイツ語の単語はデータベースから取得しますか?」という英語の単語は何ですか? それから私は同じデータベースから取った言葉で3つのランダムな答えを生成します。それにはそれに対応する答えもあります。 ;) –

+0

だから問題は何ですか? –

答えて

1

Q1のページで、正しい答えを知る必要がある場合は、その質問をセッションに入れ、次のページで取得してください。

+0

これが正しく動作するはずです。 – Apurv

関連する問題