如何在测验结束时存储和显示分数

发布于 2024-11-03 13:53:02 字数 4369 浏览 0 评论 0原文

我有一个简单的测验程序,它从数据库中随机选择 10 个问题并将其呈现给用户。在测验结束时,当用户点击提交时,我想向用户显示分数。

下面是运行测验的代码片段

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class serv3p extends HttpServlet    
{

 protected void doPost(HttpServletRequest req, HttpServletResponse res)    
                     throws ServletException, IOException    
    {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        String qKey = req.getParameter("sQuestionID");
        int iQuestionID = Integer.parseInt(qKey);


        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String sourceURL = new String("jdbc:odbc:MyQuiz");//DSN name
            Connection databaseConnection = DriverManager.getConnection(sourceURL);
            if (databaseConnection != null)
                out.println("<P>Connection made<BR>");

            if (iQuestionID < 11) out.println("QuestionID is: " + qKey);
            Statement myStatement = databaseConnection.createStatement();
            ResultSet results = myStatement.executeQuery ("SELECT TOP 10 QuestionBank.QuestionText, QuestionBank.Choice1, QuestionBank.Choice2,"
            +"QuestionBank.Choice3, QuestionBank.Choice4,QuestionBank.Answer FROM QuestionBank ORDER BY RND(QuestionID)");



            //ResultSet results = myStatement.executeQuery ("SELECT * FROM QuestionBank WHERE QuestionID = '"+qKey+"'");

            if (results.next())
                {
                String r1, r2, r3, r4, r5;
                r1 = results.getString(1);
                r2 = results.getString(2);
                r3 = results.getString(3);
                r4 = results.getString(4);
                r5 = results.getString(5);
                //r6 = results.getString(6);
                out.println("<BODY BGCOLOR='lightblue'></BODY>" +
                    "<FORM method = \"post\" action = \"http://localhost:8080/servlet/Relay\">" +
                    "<TABLE BORDER = 2 CELLPADDING = 4 CELLSPACING = 2>" +
                    "<TR><TD COLSPAN = 6 ALIGN = CENTER><H2><B>Quick Quizz : Sports</B></H2></TD></TR>");
                out.println("<TR><TD>" + r1 + "</TD></TR>");
                out.println("<TR><TD><input type = \"radio\" name = \"answer\" value = \"1\">" + r2 + "</TD></TR><BR>");
                out.println("<TR><TD><input type = \"radio\" name = \"answer\" value = \"2\">" + r3 + "</TD></TR><BR>");
                out.println("<TR><TD><input type = \"radio\" name = \"answer\" value = \"3\">" + r4 + "</TD></TR><BR>");
                out.println("<TR><TD><input type = \"radio\" name = \"answer\" value = \"4\">" + r5 + "</TD></TR><BR>");
                out.println("</TABLE>");
                results.close();
                iQuestionID++; qKey = "" + iQuestionID;
                out.println("<BR>End of Database Records<BR>");
                if (iQuestionID < 11)

                    {
                    out.println("<BR><BR><INPUT type = \"submit\" value = \"Press for Next Question\">" +
                                "<INPUT type = \"hidden\" name = \"sQuestionID\" value = '" + qKey + "'><p>" +
                                "<INPUT type = \"hidden\" name = \"choice\" value = \"Play\"></FORM></BODY></HTML>");
                    }

                else
                    {out.println("<BR><BR><INPUT type = \"submit\" value = \"Press to Exit\">" +
                                 "<INPUT type = \"hidden\" name = \"sQuestionID\" value = '" + qKey + "'><p>" +
                                 "<INPUT type = \"hidden\" name = \"choice\" value = \"serv43a\"></FORM></BODY></HTML>");

                    }
                out.close();
                }
            }
            catch(ClassNotFoundException cnfe)
                {System.out.println(cnfe);}
            catch(SQLException sqle)
                {System.out.println("Error is: " + sqle);}

    }
}

I've got a simple quiz program that selects 10 random questions from the database and presents it to the user. At the end of the quiz when the user cicks on submit, i'd like to display the score to the user.

Below is a snippet of the code that runs the quizz

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class serv3p extends HttpServlet    
{

 protected void doPost(HttpServletRequest req, HttpServletResponse res)    
                     throws ServletException, IOException    
    {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();
        String qKey = req.getParameter("sQuestionID");
        int iQuestionID = Integer.parseInt(qKey);


        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String sourceURL = new String("jdbc:odbc:MyQuiz");//DSN name
            Connection databaseConnection = DriverManager.getConnection(sourceURL);
            if (databaseConnection != null)
                out.println("<P>Connection made<BR>");

            if (iQuestionID < 11) out.println("QuestionID is: " + qKey);
            Statement myStatement = databaseConnection.createStatement();
            ResultSet results = myStatement.executeQuery ("SELECT TOP 10 QuestionBank.QuestionText, QuestionBank.Choice1, QuestionBank.Choice2,"
            +"QuestionBank.Choice3, QuestionBank.Choice4,QuestionBank.Answer FROM QuestionBank ORDER BY RND(QuestionID)");



            //ResultSet results = myStatement.executeQuery ("SELECT * FROM QuestionBank WHERE QuestionID = '"+qKey+"'");

            if (results.next())
                {
                String r1, r2, r3, r4, r5;
                r1 = results.getString(1);
                r2 = results.getString(2);
                r3 = results.getString(3);
                r4 = results.getString(4);
                r5 = results.getString(5);
                //r6 = results.getString(6);
                out.println("<BODY BGCOLOR='lightblue'></BODY>" +
                    "<FORM method = \"post\" action = \"http://localhost:8080/servlet/Relay\">" +
                    "<TABLE BORDER = 2 CELLPADDING = 4 CELLSPACING = 2>" +
                    "<TR><TD COLSPAN = 6 ALIGN = CENTER><H2><B>Quick Quizz : Sports</B></H2></TD></TR>");
                out.println("<TR><TD>" + r1 + "</TD></TR>");
                out.println("<TR><TD><input type = \"radio\" name = \"answer\" value = \"1\">" + r2 + "</TD></TR><BR>");
                out.println("<TR><TD><input type = \"radio\" name = \"answer\" value = \"2\">" + r3 + "</TD></TR><BR>");
                out.println("<TR><TD><input type = \"radio\" name = \"answer\" value = \"3\">" + r4 + "</TD></TR><BR>");
                out.println("<TR><TD><input type = \"radio\" name = \"answer\" value = \"4\">" + r5 + "</TD></TR><BR>");
                out.println("</TABLE>");
                results.close();
                iQuestionID++; qKey = "" + iQuestionID;
                out.println("<BR>End of Database Records<BR>");
                if (iQuestionID < 11)

                    {
                    out.println("<BR><BR><INPUT type = \"submit\" value = \"Press for Next Question\">" +
                                "<INPUT type = \"hidden\" name = \"sQuestionID\" value = '" + qKey + "'><p>" +
                                "<INPUT type = \"hidden\" name = \"choice\" value = \"Play\"></FORM></BODY></HTML>");
                    }

                else
                    {out.println("<BR><BR><INPUT type = \"submit\" value = \"Press to Exit\">" +
                                 "<INPUT type = \"hidden\" name = \"sQuestionID\" value = '" + qKey + "'><p>" +
                                 "<INPUT type = \"hidden\" name = \"choice\" value = \"serv43a\"></FORM></BODY></HTML>");

                    }
                out.close();
                }
            }
            catch(ClassNotFoundException cnfe)
                {System.out.println(cnfe);}
            catch(SQLException sqle)
                {System.out.println("Error is: " + sqle);}

    }
}

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

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

发布评论

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

评论(2

梅窗月明清似水 2024-11-10 13:53:02

为什么不使用数据库来存储计算出的分数,然后将其显示在结果页面上呢?如果您不需要保留分数一段时间(例如超过一个小时左右),您可以随时根据需要检查并删除旧记录。

Why not use the database to store the calculated score, and then display it on the results page? If you don't need to keep the scores for any length of time (e.g. longer than an hour or so), you can always check for, and delete old records as often as necessary.

汐鸠 2024-11-10 13:53:02

那么您应该问自己的问题是,我是否想将分数保留一段时间(例如:民意调查),或者您只是想将其用作类似测试的软件,在测试后返回准确的分数测试,但不仅仅是将该值与软件结束一起销毁,

如果您确实想将其保留更长时间,您可以将所有值写入 Access 数据库,甚至 SQL 服务器中,

如果您认为你想要保留的价值观并不多我建议你把它写在一个基于 txt 的文件中,因为它很容易存储,并且不需要像数据库连接那样花费太多时间来写它,

为了得到你的分数,你需要硬编码正确的答案,使用 swich case 或 if/else,只需用正确答案对字符串的位置进行硬编码,然后将分数添加到私有 int (或 float/double),

显示它并不难,如果您想显示来自的内容很多操作参与者,只需使用增强型循环,使用 println 显示参与者的姓名和号码,甚至将其放入 JFrame 中。这取决于你,


我的建议

(你想保留很多数据)=> SQL /(访问)数据库

(您想要保留的一些数据)=>如果您知道参与者的确切数量(XML)

(只需 diplay)打印您的分数,

您将需要对此分数进行硬编码,这取决于您选择值的方式。


最后提示:不要打开太多数据库连接,它只会减慢你的进度,像正确答案这样的东西很容易硬编码,因为它们不是变量,而且你只有大约 10 个问题


问题 1:(编辑)

没问题,你只想要 10 个问题正确,因为我昨晚和我的一个朋友讨论了这个问题,如果你确实想将分数保留更长时间,你可能希望有可能添加更多问题(如果不是) ,

只需对每个使用 if else 方式问题

if(Question1.equals("Question1"))
{
没问题,你只需要 10 个问题就对了​​,因为我昨晚和我的一个朋友讨论了这个问题,如果你确实想保留分数更长的时间,你可能希望有可能添加更多如果没有问题,

只需对每个问题使用 if else 方式

if(Question1.equals("Question1"))

{

Switch(answer)

case 1: points++;休息;

情况2:中断;

情况3:断线;

默认:中断;

}

}

else

{

if(Question2.equals("Question2"))

{

Switch(answer)

case 1:break;

情况2:积分++;休息;

情况3:中断;

默认:中断;

}

else

{

}

}

等等,像这样填写问题,

String Question = "Question1";

if ( Question.equals("Question1"))
{
...
如果您

确实希望使用数据库来完成此操作,您也可以将答案放入额外的属性中,并且可以像这样

int[] answerOnTheQuestions = 测试中的答案(将它们放入 Int 数组中)
int[] 答案=(您的数据库链接到此处的正确表和列)
分数 = 0;

for ( int counter = 0; counter < questions.length; counter ++)

{

if(answerOnTheQuestions[counter] ==answers[counter] )

{

Score++; }

分数

与可在 GUI 或控制台中获取的参与者姓名一起写入表中。

我希望这有帮助:)

Well the question you should ask yourself is, do i want to keep the scores for an amount of time ( for an example: a poll ), or do you just want to use it as a Test like software wich returns the exact score after the test, but than just destroys that value together with the end of the software,

if you do want to keep it for a longer amount of time, you could write all values in an access database, or even an SQL server,

if you think the values you want to keep aren't that mutch, than i'd sugest you write it down in a txt based file, since it is easy to store and doesn't take as much time as a database connection to write it away,

to get at your score you will need to hardcode the right answers, with swich cases or if/else, just hardcode the place of your string with te correct answer, and add the score to a private int (or float/double)

displaying it is not that hard, if you want to display the stuff from plenty op participants, just use an enhanced for loop, with a println wich displays the name of the participant and the numbers, or even put it in a JFrame. thats up to you,


my advise

( a lot of data you want to keep ) => SQL / (access) databases

( a bit of data you want to keep ) => txt File and if you know the exact number of participants ( XML )

( just diplay ) print your score,

you will need to hardcode this score, and that depends on how you chose the values.


final tip: Dont open too many db connections it will only slow your progress down, and stuff like the right answer are easy to hard code since they are not variable, and you only have like 10 questions


Question 1: (edit)

no problem, you only want like 10 Questions right, cause i discussed the problem with a friend of mine last night, and if you do want to keep the scores for a longer amount of time, you might want to have the possibility to add more questions if not,

just use the if else way for each question

if(Question1.equals("Question1"))
{
no problem, you only want like 10 Questions right, cause i discussed the problem with a friend of mine last night, and if you do want to keep the scores for a longer amount of time, you might want to have the possibility to add more questions if not,

just use the if else way for each question

if(Question1.equals("Question1"))

{

Switch(answer)

case 1: points++; break;

case 2: break;

case 3: break;

default: break;

}

}

else

{

if(Question2.equals("Question2"))

{

Switch(answer)

case 1: break;

case 2: points++; break;

case 3: break;

default: break;

}

else

{

}

}

and so on, fill in the Questions like this,

String Question = "Question1";

if ( Question.equals("Question1"))
{
...
}

if you do wish to do it with a database, you can as well put the answer in an extra attribute, and than you could do it like this

int[] answerOnTheQuestions = the answers from the test ( put them in an Int array )
int[] answers = ( your db link to the right table and column here )
int score = 0;

for ( int counter = 0; counter < questions.length; counter ++)

{

if( answersOnTheQuestions[counter] == answers[counter] )

{

score++;

}

}

Write score to your table together with like a participants name that you can get in the GUI or consle.

I hope this helped :)

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