1.计划
1.1需求描述:
现在市场上有很多的面向小学生的题卡,但是这习题卡不但价格昂贵,而且每次做题的内容基本都是固定。针对这些问题,开发出了这款网页在线答题系统,每次的题目都有所不同,可以跟快更好提高小学生的计算能力。
1.2估计开发时间
一天两宿。
1.3填写项目数据
产生随机的算式以及结果,保存在数据库中;
在程序运行过程中,需要用户填写产生题目的数量,产生题目后,系统会自动的给出评判结果。
1.4填写预估时间记录日志
学生:孙丙海 日期:2017.12.06
教师:王建民
课程:psp
日期 |
开始时间 |
结束时间 |
中断时间 |
净时间 |
活动 |
备注 |
C |
U |
12/3 |
18:00 |
21:00 |
|
3小时 |
调试程序 |
完成底层代码 |
|
|
12/4 |
15:00 |
17:00 |
|
2小时 |
调试程序 |
编写jsp |
|
|
12/5 |
11:00 |
12:00 |
|
1小时 |
调试程序 |
设计程序中的倒计时功能 |
|
|
1.5填写实际时间记录日志
学生:孙丙海 日期:2017.12.06
教师:王建民
课程:psp
日期 |
开始时间 |
结束时间 |
中断时间 |
净时间 |
活动 |
备注 |
C |
U |
12/3 |
18:00 |
22:00 |
|
4小时 |
调试程序 |
完成底层代码 |
|
|
12/4 |
15:00 |
17:00 |
|
2小时 |
调试程序 |
编写jsp |
|
|
12/5 |
11:00 |
11:40 |
|
1.8小时 |
调试程序 |
设计程序中的倒计时功能 |
|
|
- 开发
2.1程序设计思想
首先实现用代码生成随机的加减乘除的算式以及结果,并存入到数据库中,在运行web端的程序,弹出输入做的提的数目,并将该数量提交到另外一个答题界面中,并显示题目,让使用者输入计算的结果,并实现在线答题,控制答题的时间,超出时间自动跳转,并告诉使用者对错情况,并统计使用者作对以及做错的数量,并在后面输出相应的笑脸或哭脸。
代码:
package after_class;
public class AddAndMultiply
{
private int firstNum = 0;
private int secondNum = 0;
private int result = 0;
private char operator = 0;
public int getFirstNum() {
return firstNum;
}
public void setFirstNum(int firstNum) {
this.firstNum = firstNum;
}
public int getSecondNum() {
return secondNum;
}
public void setSecondNum(int secondNum) {
this.secondNum = secondNum;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public char getOperator() {
return operator;
}
public void setOperator(char operator) {
this.operator = operator;
}
}
package after_class;
//加减(可以产生三元的表达式)
public class AddAndReduce
{
private int firstNum = 0;
private int secondNum = 0;
private int thirdNum = 0;
private int result = 0;
private char firstOperator = 0;
private char secondOperator = 0;
public int getFirstNum() {
return firstNum;
}
public void setFirstNum(int firstNum) {
this.firstNum = firstNum;
}
public int getSecondNum() {
return secondNum;
}
public void setSecondNum(int secondNum) {
this.secondNum = secondNum;
}
public int getThirdNum() {
return thirdNum;
}
public void setThirdNum(int thirdNum) {
this.thirdNum = thirdNum;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public char getFirstOperator() {
return firstOperator;
}
public void setFirstOperator(char firstOperator) {
this.firstOperator = firstOperator;
}
public char getSecondOperator() {
return secondOperator;
}
public void setSecondOperator(char secondOperator) {
this.secondOperator = secondOperator;
}
}
package after_class;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Random;
import util.DBUtil;
public class CreateAddAndMultiplyRandom
{
public static void CreateAddAndMultiplyOperation(int times) throws SQLException//此处的times指的是产生的式子的个数
{
AddAndMultiply once = new AddAndMultiply();
for(int i = 0;i<times;i++)
{
once = CreateOnceOperation();
while(checkAddAndMultiply(once))
{
once = CreateOnceOperation();
}
}
}
//以下部分为存储到数据库中,并且查重。如果重复,返回true。如果不重复,返回false
private static boolean checkAddAndMultiply(AddAndMultiply once)
{
String storeOperation = String.valueOf(once.getFirstNum())+String.valueOf(once.getOperator())+String.valueOf(once.getSecondNum());
String storeResult = String.valueOf(once.getResult());
//获得链接对象
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
//准备sql语句
String sql = "select count(*) from t_operation where storeOperation = ? ";
//创建语句传输对象
ResultSet resultSet = null;
try
{
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, storeOperation);
//接收结果集
resultSet = preparedStatement.executeQuery();
//遍历结果集
while(resultSet.next())
{
if (resultSet.getInt(1) > 0)
{
return true;
}
}
sql = "insert into t_operation(storeOperation,storeResult) values(?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, storeOperation);
preparedStatement.setString(2, storeResult);
preparedStatement.executeUpdate();
return false;
}
catch(Exception e)
{
e.printStackTrace();
}
return false;
}
private static AddAndMultiply CreateOnceOperation() throws SQLException
{
Random random = new Random();
AddAndMultiply once = new AddAndMultiply();
char operator = 0;
int firstNum = 0;
int secondNum = 0;
int result = 0;
int selectOperator = random.nextInt(4)+1;
if(selectOperator==1)
{
operator = '+';
firstNum = random.nextInt(99)+1;
secondNum = random.nextInt(100-firstNum)+1;
result = firstNum+secondNum;
}
else if(selectOperator==2)
{
operator = '-';
firstNum = random.nextInt(100)+1;
secondNum = random.nextInt(firstNum)+1;
result = firstNum-secondNum;
}
else if(selectOperator==3)
{
operator = '*';
firstNum = random.nextInt(9)+1;
secondNum = random.nextInt(9)+1;
result = firstNum*secondNum;
}
else
{
operator = '/';
firstNum = random.nextInt(9)+1;
secondNum = random.nextInt(9)+1;
firstNum = secondNum*firstNum;
result = (int)firstNum/(int)secondNum;
}
once.setFirstNum(firstNum);
once.setOperator(operator);
once.setSecondNum(secondNum);
once.setResult(result);
return once;
}
}
package after_class;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Random;
import util.DBUtil;
//产生三元的表达式
public class CreateAddRandom
{
public static void createAddOperation(int times) throws SQLException
{
AddAndReduce once = new AddAndReduce();
for(int i = 0;i<times;i++)
{
once = CreateOnceOperation();
while(checkAddAndReduce(once))
{
once = CreateOnceOperation();
}
}
}
//以下部分为存储到数据库中,并且查重。如果重复,返回true。如果不重复,返回false
private static boolean checkAddAndReduce(AddAndReduce once)
{
String storeOperation = String.valueOf(once.getFirstNum())+String.valueOf(once.getFirstOperator())
+String.valueOf(once.getSecondNum())+String.valueOf(once.getSecondOperator())
+String.valueOf(once.getThirdNum());
String storeResult = String.valueOf(once.getResult());
//获得链接对象
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
//准备sql语句
String sql = "select count(*) from t_operation2 where storeOperation = ? ";
//创建语句传输对象
ResultSet resultSet = null;
try
{
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,storeOperation);
//接收结果集
resultSet = preparedStatement.executeQuery();
//遍历结果集
while(resultSet.next())
{
if (resultSet.getInt(1) > 0)
{
return true;
}
}
sql = "insert into t_operation2(storeOperation,storeResult) values(?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, storeOperation);
preparedStatement.setString(2, storeResult);
preparedStatement.executeUpdate();
return false;
}
catch(Exception e)
{
e.printStackTrace();
}
return false;
}
private static AddAndReduce CreateOnceOperation() throws SQLException
{
Random random = new Random();
AddAndReduce once = new AddAndReduce();
int firstNum = 0;
int secondNum = 0;
int thirdNum = 0;
int result = 0;
char firstOperator = 0;
char secondOperator = 0;
//首先随机产生两个数,并且把第二个数作为后两个数的上限
int bound = 0;//这个bound就是界限
int selectOperator = random.nextInt(4)+1;
if(selectOperator==1)
{
firstOperator = '+';
secondOperator = '+';
firstNum = random.nextInt(98)+1;
bound = 100-firstNum;
secondNum = random.nextInt(bound-1)+1;
thirdNum = random.nextInt(bound-secondNum)+1;
result = firstNum+secondNum+thirdNum;
}
else if(selectOperator==2)
{
firstOperator = '+';
secondOperator = '-';
firstNum = random.nextInt(99)+1;
bound = 100-firstNum;
secondNum = random.nextInt(bound)+1;
thirdNum = random.nextInt(firstNum+secondNum)+1;
result = firstNum+secondNum-thirdNum;
}
else if(selectOperator==3)
{
firstOperator = '-';
secondOperator = '+';
//此处不需要bound
firstNum = random.nextInt(100)+1;
secondNum = random.nextInt(firstNum)+1;
thirdNum = random.nextInt(100-(firstNum-secondNum))+1;
result = firstNum-secondNum+thirdNum;
}
else
{
firstOperator = '-';
secondOperator = '-';
firstNum = random.nextInt(99)+2;
secondNum = random.nextInt(firstNum-1)+1;
thirdNum = random.nextInt(firstNum-secondNum)+1;
result = firstNum-secondNum-thirdNum;
}
once.setFirstNum(firstNum);
once.setFirstOperator(firstOperator);
once.setSecondNum(secondNum);
once.setSecondOperator(secondOperator);
once.setThirdNum(thirdNum);
once.setResult(result);
return once;
}
}
package after_class;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import dao.IOperationDao;
import dao.Operation;
import dao.OperationDaoImpl;
public class CreateAll
{
public static void main(String[] args)
{
Scanner in =new Scanner(System.in);
System.out.println("请输入要产生的题目的数量:");
int number = in.nextInt();
try {
CreateAddRandom.createAddOperation(number);
CreateAddAndMultiplyRandom.CreateAddAndMultiplyOperation(number);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("OK,产生成功!");
}
}
package dao;
import java.util.List;
public interface IOperationDao
{
//这个函数是用来调取加减三元中的数据,times是要提取式子的数量
public List<Operation> loadAdd(int times);
//这个函数是用来调取加减乘除表中的数据,times是要提取式子的数量
public List<Operation> loadMultiply(int times);
}
package dao;
public class Operation
{
private int ID;
private String storeOperation;
private String storeResult;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getStoreOperation() {
return storeOperation;
}
public void setStoreOperation(String storeOperation) {
this.storeOperation = storeOperation;
}
public String getStoreResult() {
return storeResult;
}
public void setStoreResult(String storeResult) {
this.storeResult = storeResult;
}
}
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import util.DBUtil;
public class OperationDaoImpl implements IOperationDao
{
public List<Operation> loadAll(int times)
{
ArrayList<Operation> list = new ArrayList<Operation>();
int createMultiply = (times/10)*9+1;
int createAdd =times-createMultiply;
list.addAll(loadMultiply(createMultiply));
if(createAdd!=0)
{
list.addAll(loadAdd(createAdd));
}
return list;
}
//从加减三元中获取数据
public ArrayList<Operation> loadAdd(int times)
{
ArrayList<Operation> list = new ArrayList<Operation>();
Operation Operation = new Operation();
//作为产生算式的数量
Random random = new Random();
int previous = random.nextInt(5000)+1;
for(int i = 0;i<times;i++)
{
Operation = loadAddById(previous);
list.add(Operation);
previous = (2*previous+1)%5000;
}
return list;
}
//从加减三元中获取数据
public Operation loadAddById(int id)
{
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from t_operation2 where ID=?";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Operation Operation = null;
try
{
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
while(resultSet.next())
{
Operation = new Operation();
Operation.setID(resultSet.getInt("ID"));
Operation.setStoreOperation(resultSet.getString("storeOperation"));
Operation.setStoreResult(resultSet.getString("storeResult"));
}
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return Operation;
}
public Operation loadMultiplyById(int id)
{
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from t_operation where ID=?";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Operation Operation = null;
try
{
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
while(resultSet.next())
{
Operation = new Operation();
Operation.setID(resultSet.getInt("ID"));
Operation.setStoreOperation(resultSet.getString("storeOperation"));
Operation.setStoreResult(resultSet.getString("storeResult"));
}
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return Operation;
}
//从加减乘除表中获取数据
public ArrayList<Operation> loadMultiply(int times)
{
ArrayList<Operation> list = new ArrayList<Operation>();
Operation Operation = new Operation();
//作为产生算式的数量
Random random = new Random();
int previous = random.nextInt(5000)+1;
for(int i = 0;i<times;i++)
{
Operation = loadMultiplyById(previous);
list.add(Operation);
previous = (2*previous+1)%5000;
}
return list;
}
}
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
public static Connection getConnection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String user = "sa";
String password = "jisuan@10Q";
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=OnClass";
Connection connection = null;
try {
connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection ) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement ) {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet resultSet ) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<embed src="./1/1.mp3" hidden=true autostart=true loop=true></embed>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>四则运算在线答题系统</title>
<style type="text/css">
.asd{color:#44f40d}
.a{
width:300px;
height:200px;
position:fixed;
top:50%;
left:50%;
margin-top:-230px;
margin-left:-20px;
}
</style>
</head>
<body style="background-image: url(./1/1.jpg); background-size:cover; ">
<div1 class="a">
<form id="form1" name="form1" method="get" action="main.jsp">
<div>
<p align="center" class="asd" >四则运算在线答题登录</p>
</div>
<table width="296" border="1" align="center" bgcolor="#00FF99">
<tr>
<td width="98" height="34">生成题目数量</td>
<td width="182"><label> <input name="num"
type="text" id="user_name" />
</label></td>
</tr>
<tr>
<td height="36">所用时间</td>
<td> <input name="user_pass1" type="text"
id="user_pass1" value="60秒">
</td>
</tr>
<tr>
<td height="35" colspan="2" align="center"><label>
<input type="submit" name="Submit" value="开始答题" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
//从隐藏的标签中获取数据
String[] result = request.getParameterValues("result");
String[] operation = request.getParameterValues("operation");
String[] answer = request.getParameterValues("answer");
%>
<html>
<head>
<style type="text/css">
.c
{
font-weight:bold;
}
</style>
</head>
<body style="background-image: url(./1/2.jpg); background-size:cover;">
<div class="c">
<table align="center" width="600">
<tr>
<td width="20%">题号</td><br>
<td >题目</td><br>
<td>计算结果</td><br>
<td>答案</td><br>
<td>正误</td><br>
</tr>
<%
int i=1;
int correctNum = 0;
for(String once:result)
{
%>
<tr>
<td><%=i %></td>
<td><%=operation[i-1]%></td>
<%
if(result[i-1].equals(answer[i-1].trim()))
{
correctNum++;
%>
<td>
<font style = "color:green">
<b><%=result[i-1]%></b>
</font>
</td>
<td><%=answer[i-1]%></td>
<td>
<img src="./1/4.png">
</td>
<%
}
else
{
%>
<td>
<font style = "color:red">
<%=result[i-1]%>
</font>
</td>
<td><%=answer[i-1]%></td>
<td>
<img src="./1/5.png">
</td>
<%
}
%>
</tr>
<%
i++;
}
%>
</table>
<br><a href="1.jsp">再来一次</a>
</div>
</body>
</html>
<%@page import="dao.OperationDaoImpl"%>
<%@page import="dao.IOperationDao"%>
<%@page import=" java.util.ArrayList" %>
<%@page import=" java.util.List" %>
<%@page import=" java.util.*" %>
<%@page import="dao.Operation "%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.a
{
font-weight:bold;
}
.b
{
font-weight:bold;
color:#F00
}
</style>
<SCRIPT LANGUAGE="JavaScript">
var maxtime = 60 //半个小时,按秒计算,自己调整!
function changeColor(){
var color="#f00|#0f0|#00f|#880|#808|#088|yellow|green|blue|gray";
color=color.split("|");
document.getElementById("timer").style.color=color[parseInt(Math.random() * color.length)];
}
setInterval("changeColor()",200);
function CountDown()
{
if(maxtime>=0)
{
seconds = Math.floor(maxtime%60);
msg = "距离结束还有"+seconds+"秒";
document.all["timer"].innerHTML=msg;
if(maxtime == 5)
{
alert('注意,还有5秒!');
}
--maxtime;
}
else
{
clearInterval(timer);
alert("时间到,结束!");
window.location.href="checkResult.jsp";
}
}
timer = setInterval("CountDown()",1000);
</SCRIPT>
</head>
<body style="background-image: url(./1/3.jpg); background-size:cover;">
<div id="timer" style="color:ffff00" align="center" class="a"></div>
<%
//此处的参数用来说明要产生的式子的个数
int number=Integer.parseInt(request.getParameter("num"));
//对需要的对象进行初始化
OperationDaoImpl operationDaoImpl=new OperationDaoImpl();
List<Operation> list=new ArrayList<Operation>();
Operation operation=new Operation();
//从数据库中提取数据,并且给list赋值方便输出
list = operationDaoImpl.loadAll(number);
%>
<div class="b">
<form action="checkResult.jsp" method="get" color="red">
<table align="center" border="1" width="600">
<tr>
<td width="20%">题号</td><br>
<td >题目</td><br>
<td>计算结果</td><br>
</tr>
<%
int i=1;
for(Operation once:list)
{
%>
<tr>
<td><%=i %></td>
<td><%=once.getStoreOperation()%></td>
<input type="hidden" name="operation" value=<%=once.getStoreOperation()%>>
<input type="hidden" name="answer" value=<%=once.getStoreResult()%>>
<td>
<input style="text" name="result"/>
</td>
</tr>
<%
i++;
}
%>
<tr>
<td colspan="4" align="center">
<input type="submit" value="提交试卷" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
运行结果截图: