要求:
实现简单的在线测试功能,题型包括单选、多选和判断题,考试完成提交答案,目标页面对答案进行判断和分数统计。
第一个jsp文件用于展示题目及可选答案,填写完成后将表单提交至第二个jsp文件;
第二个jsp文件接收前一个页面的jsp表单数据,并对用户输入答案与正确答案作对比,给出相应得分,此文件封装了一个判断类,将用户答案,正确答案以及分数作为私有变量,进行判断分数,最后累加每一题的分数,alert弹出分数。
页面表单展示代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>题目测试</title>
</head>
<body>
<form method="post" action="testCheck1.jsp">
<h5>一、单选题</h5>
1.对一台计算机来说,( )的档次就基本上决定了整个计算机的档次。<br>
<input type="radio" name="1" value="A">A、内存<br>
<input type="radio" name="1" value="B">B、主机<br>
<input type="radio" name="1" value="C">C、硬盘<br>
<input type="radio" name="1" value="D">D、CPU<br>
2.毛泽东思想的精髓是()。<br>
<input type="radio" name="2" value="A">A、实事求是<br>
<input type="radio" name="2" value="B">B、群众路线<br>
<input type="radio" name="2" value="C">C、独立自主<br>
<input type="radio" name="2" value="D">D、艰苦奋斗<br>
3.在拆装微机前,应该释放掉手上的()。<br>
<input type="radio" name="3" value="A">A、静电<br>
<input type="radio" name="3" value="B">B、手机<br>
<input type="radio" name="3" value="C">C、工具<br>
<input type="radio" name="3" value="D">D、泥渍<br>
<h5>二、判断题</h5>
4.只有已经安装并配置了适当的驱动程序,操作系统才能够使用该设备。( )<br>
<input type="radio" name="4" value="Y">正确<br>
<input type="radio" name="4" value="N">错误<br>
5.马克思主义中国化已有五个理论成果。()<br>
<input type="radio" name="5" value="Y">正确<br>
<input type="radio" name="5" value="N">错误<br>
6.CPU的总线包括数据总线、地址总线和控制总线。()<br>
<input type="radio" name="6" value="Y">正确<br>
<input type="radio" name="6" value="N">错误<br>
<h5>三、多选题</h5>
7.职业道德的基本要求有?( ) <br>
<input type="checkbox" name="7" value="A">A、爱岗敬业<br>
<input type="checkbox" name="7" value="B">B、诚实守信<br>
<input type="checkbox" name="7" value="C">C、办事公道<br>
<input type="checkbox" name="7" value="D">D、服务群众<br>
<input type="checkbox" name="7" value="E">E 、奉献社会<br>
8.群众路线是()<br>
<input type="checkbox" name="8" value="A">A、一切为了群众<br>
<input type="checkbox" name="8" value="B">B、一切依靠群众<br>
<input type="checkbox" name="8" value="C">C、从群众中来<br>
<input type="checkbox" name="8" value="D">D、到群众中去<br>
9.计算机主机是()的总称。()<br>
<input type="checkbox" name="9" value="A">A、控制器<br>
<input type="checkbox" name="9" value="B">B、运算器<br>
<input type="checkbox" name="9" value="C">C、存储器<br>
<input type="checkbox" name="9" value="D">D、寄存器<br>
<P/>
<input type="submit" value="完成答题">
</form>
</body>
</html>
验证答案页面代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>题目测试2</title>
</head>
<body>
<%
//分数判断类
class checkExam {
private String[] str1; //用于存储多选题返回的字符串数组的卷面答案
private String str2; //用于存储单选题、判断题的卷面答案
private String Right; //用于存储正确答案
private int score; //用于存储单选题、判断题分数,以及多选题分数(多选题选错不得分,答错得一半分数)
//构造函数,用于多选题
public checkExam(String[] str1, String Right, int score) {
this.str1 = str1;
this.Right = Right;
this.score = score;
}
//构造函数,用于单选题、判断题
public checkExam(String str2, String Right, int score) {
this.str2 = str2;
this.Right = Right;
this.score = score;
}
//判断多选题答案及返回最终得分
private int check1() {
if (str1 == null || str1.length == 0)
return 0;
int length2 = Right.length();
int length1;
int sum = 0;
while (length2 > 0) {
length1 = str1.length;
while (length1 > 0) {
if (String.valueOf(Right.charAt(Right.length() - length2)).equals(str1[str1.length - length1])) {
sum++;
}
length1--;
}
length2--;
}
length1 = str1.length;
while (length1 > 0) {
length2 = Right.length();
int n = 0;
while (length2 > 0) {
if (str1[str1.length - length1].equals(String.valueOf(Right.charAt(Right.length() - length2)))) {
// System.out.println(str1[str1.length - length1]+","+String.valueOf(str2.charAt(str2.length() - length2)));
n++;
}
length2--;
}
if (n == 0)
return 0;
length1--;
}
if (sum > 0 && sum < Right.length())
return score / 2;
else if (sum == Right.length())
return score;
else
return 0;
}
//判断单选题、判断题答案及返回最终得分
private int check2() {
if (Right == null)
return 0;
else if (Right.equals(str2))
return score;
else
return 0;
}
//综合单选、判断、多选题判断,返回得分
int check() {
//根据Right正确答案的长度判断题目为单选、判断或多选
if (Right.length() == 1)
return check2();
else if(Right.length() > 1)
return check1();
else
return 0;
}
}
String t1 = request.getParameter("1");
String t2 = request.getParameter("2");
String t3 = request.getParameter("3");
String t4 = request.getParameter("4");
String t5 = request.getParameter("5");
String t6 = request.getParameter("6");
String[] t7 = request.getParameterValues("7");
String[] t8 = request.getParameterValues("8");
String[] t9 = request.getParameterValues("9");
int num = 0; //最终得分
num += new checkExam(t1, "D", 10).check();
//第一个参数为之前页面表单提交的答案,第二个参数为题目的正确答案,第三个参数为题目的总分数
num += new checkExam(t2, "A", 10).check();
num += new checkExam(t3, "A", 10).check();
num += new checkExam(t4, "Y", 10).check();
num += new checkExam(t5, "Y", 10).check();
num += new checkExam(t6, "Y", 10).check();
num += new checkExam(t7, "ABCDE", 13).check();
num += new checkExam(t8, "ABCD", 13).check();
num += new checkExam(t9, "ABC", 14).check();
%>
<script>
alert("完成答题,您的最终得分为:<%=num%>");
</script>
</body>
</html>