程序设计思想:
首先实现用代码生成随机的加减乘除的算式以及结果,并存入到数据库中,在运行web端的程序,弹出输入做的提的数目,并将该数量提交到另外一个jsp中,并显示题目,让使用者输入计算的结果,并实现在线答题,控制答题的时间,超出时间自动跳转,并告诉使用者对错情况,并统计使用者作对以及做错的数量。
程序源代码:
Success.jsp
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@ 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>
<style type="text/css">
<!--
.STYLE2 {
font-family: "Times New Roman", Times, serif;
font-weight: bold;
}
-->
</style>
<body style=" background-image:url(../img/dj.jpg) ">
<div>
<form action="list1.jsp" method="get">
<br>
<p align="center"> 愿你生活每一天都开心</p>
<hr>
<br><br><br><br><br><br>
<table height="133" border="0" align="center">
<tr>
<td height="29"><span class="STYLE2">做题时间</span></td>
<td><textarea name="textarea" rows="1"></textarea></td>
</tr>
<tr>
<td width="107" height="28"><strong>
产生题的数量 </strong></td>
<td width="190"><textarea name="number" rows="1" style="border:0px background:none "></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="submit" type="submit" style="width:300px; height:66px;" value ="开始答题"/></td>
</tr>
</table>
<div align="center">
<%
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currenTime=new Date();
String time=simpleDateFormat.format(currenTime).toString();
out.print("当前时间:"+time);
%>
</div>
<br><br><br><br><br><br><br><br><br><br>
<p align="center">TTF处女作品</p>
<p align="center"> ©2017-325美少女战士小分队 版权所有</p>
<hr>
</body>
</html>
List.jsp
<%@page import="java.util.Random"%>
<%@page import="com.jaovo.msg.model.Exercise"%>
<%@page import="com.jaovo.msg.dao.ExerciseDaoImpl"%>
<%@page import=" java.util.ArrayList" %>
<%@page import=" java.util.List" %>
<%@ 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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<SCRIPT LANGUAGE="JavaScript">
var maxtime = 60 //半个小时,按秒计算,自己调整!
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("时间到,结束!");
document.getElementById('Tj').click();
}
}
timer = setInterval("CountDown()",1000);
</SCRIPT>
<style type="text/css">
<!--
body,td,th {
font-family: Courier New, Courier, monospace;
}
-->
</div>
</div>
.STYLE1 {color: #F0F0F0}
.STYLE1 {color: #FF0000}
.STYLE2 {color: #FFFFFF}
</style></head>
<body style=" background-image:url(../img/jyj.jpg) ">
<div >
<div id="timer" style="color:red" align="center">
<div align="center">
<%
int a=Integer.parseInt(request.getParameter("number"));
ExerciseDaoImpl exerciseDaoImpl=new ExerciseDaoImpl();
List<Exercise>list=new ArrayList<Exercise>();
Exercise exercise=new Exercise();
String id="";
String result="";
Random random=new Random();
for(int i=0;i<a/2;i++)
{
int b=random.nextInt(98)+1;
id=id+" "+b;
exercise=exerciseDaoImpl.loadTest(b);
list.add(exercise);
}
for(int j=a/2;j<a;j++)
{
int c=random.nextInt(98)+1;
id=id+" "+c;
exercise=exerciseDaoImpl.loadTest1(c);
list.add(exercise);
}
session.setAttribute("id", id);
%>
</div>
</div>
<form action="checkresult.jsp" method="get">
<p> </p>
<table width="896" border="0" align="center" >
<tr bordercolor="#333333" >
<td width="162">
<p align="center" class="STYLE1" > 编号 </p> </td>
<td width="370">
<div align="center"><span class="STYLE1">题目 </span></div></td>
<td width="350">
<div align="center"><span class="STYLE1">结果 </span></div></td>
</tr>
<%
int i=1;
for(Exercise exercise1:list){
%>
<tr bordercolor="#999999" >
<td>
<div align="center"><span class="STYLE2"><%=i %> </span></div></td>
<td>
<div align="center"><span class="STYLE2"><%=exercise1.getExperssion() %> </span></div></td>
<td bordercolor="#FFFFFF">
<input name="result" style="border:1px; background:green ;" size="50" /> </td>
</tr>
<%
i++;
}
// session.setAttribute("loginUser",user);
%>
<tr bordercolor="#0000FF">
<td colspan="4" align="center" bordercolor="#FFFFFF"> </td>
</tr>
<tr bordercolor="#FFFFFF">
<td colspan="4" align="center"> </td>
</tr>
<tr bordercolor="#FFFFFF">
<td colspan="4" align="center">
<input type="submit" value="提交" name="Tj" id="Tj" style="width:300px; height:30px;"/> </td>
</tr>
</table>
</form>
</div>
</body>
</html>
Checkresult.jsp
<%@page import="com.jaovo.msg.dao.ExerciseDaoImpl"%>
<%@page import="com.jaovo.msg.model.Exercise"%>
<%@page import=" java.util.ArrayList" %>
<%@page import=" java.util.List" %>
<%@ 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>
<style type="text/css">
<!--
.STYLE1 {color: #FFFFFF}
.STYLE2 {color: #FF0000}
.STYLE3 {color: #FF0000; font-family: Georgia, "Times New Roman", Times, serif; }
-->
</style>
<body style=" background-image:url(../img/cwj.jpg) ">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%
int m=0;//记录正确的题数;
int n=0;//记录错误的题数;
Exercise exercise=new Exercise();
ExerciseDaoImpl exerciseDaoImpl=new ExerciseDaoImpl();
List<Exercise>list=new ArrayList<Exercise>();
String id=(String)session.getAttribute("id");
String result[]=request.getParameterValues("result");
String Id[]=id.toString().trim().split(" ");
for(int i=0;i<Id.length/2;i++)
{
exercise=exerciseDaoImpl.loadTest(Integer.parseInt(Id[i]));
list.add(exercise);
System.out.println(Id[i]);
}
for(int i=Id.length/2;i<Id.length;i++)
{
exercise=exerciseDaoImpl.loadTest1(Integer.parseInt(Id[i]));
list.add(exercise);
}
for(int j=0;j<result.length;j++)
{
System.out.println(result[j]);
}
%>
<div >
<table align="center" border="0" style="background:rgba(0,0,0,0)">
<tr>
<td width="36">
<div align="center"><span class="STYLE1">编号</span> </div></td>
<td width="231">
<div align="center"><span class="STYLE1">题目 </span></div></td>
<td width="117">
<div align="center"><span class="STYLE1">结果 </span></div></td>
<td width="206">
<div align="center"><span class="STYLE1">正确结果 </span></div></td>
</tr>
<%
int i=0;
for(Exercise exercise1:list){
%>
<tr>
<td>
<div align="center"><span class="STYLE1"><%=i+1 %> </span></div></td>
<td><div align="center"><span class="STYLE1"><%=exercise1.getExperssion() %></span></div></td>
<td>
<div align="center"><span class="STYLE1">
<%
if(result[i].equals(list.get(i).getResult()))
{
m++;
%>
<font style="color:green ; font-size">
<% out.print(result[i]);%>
</font>
<%
}
else
{
n++;
%>
<font style="color:red ; font-size">
<% out.print(result[i]);%>
</font>
<%
}
%>
</span> </div></td>
<td>
<div align="center"><span class="STYLE1"><%=exercise1.getResult() %></span> </div></td>
</tr>
<%
i++;
}
// session.setAttribute("loginUser",user);
session.invalidate();
%>
</table>
<br>
<br>
<br>
<table align="center" border="0">
<tr>
<td>
<p align="center" class="STYLE2" color="white">答对的题目</p> </td>
<td>
<div align="center" class="STYLE3">答错的题目 </div></td>
</tr>
<tr bordercolor="#F0F0F0" bgcolor="#666666">
<td>
<input name="right" style="text" value="<%=m%>"/> </td>
<td>
<input name="fault" style="text" value="<%=n%>"/> </td>
</tr>
</table>
</body>
</html>
主类中的程序:
package com.jaovo.msg.model;
public class Exercise {
private int id;
private String experssion;
private String result;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getExperssion() {
return experssion;
}
public void setExperssion(String experssion) {
this.experssion = experssion;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
连接数据库:
package com.jaovo.msg.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 {
//1 鍔犺浇椹卞姩
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 = "123456";
String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=jaovo_msg";
Connection connection = null;
try {
//2 鍒涘缓閾炬帴瀵硅薄connection
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();
}
}
}
从数据库中抽题:
package com.jaovo.msg.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.model.Exercise;
public class ExerciseDaoImpl {
public Exercise loadTest(int id) {
Connection connection = DBUtil.getConnection();
//鍑嗗sql璇彞
String sql = "select * from Table1 where id = ?";
//鍒涘缓璇彞浼犺緭瀵硅薄
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Exercise exercise = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
exercise = new Exercise();
exercise.setId(id);
exercise.setExperssion(resultSet.getString("expression"));
exercise.setResult(resultSet.getString("result"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return exercise;
}
//获取三位数字;
public Exercise loadTest1(int id) {
Connection connection = DBUtil.getConnection();
//鍑嗗sql璇彞
String sql = "select * from Table2 where id = ?";
//鍒涘缓璇彞浼犺緭瀵硅薄
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Exercise exercise = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
exercise = new Exercise();
exercise.setId(id);
exercise.setExperssion(resultSet.getString("expression"));
exercise.setResult(resultSet.getString("result"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return exercise;
}
}
产生随机算式的程序:
package com.jaovo.msg.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) throws SQLException {
// TODO Auto-generatesd method stub
String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//��������
String connectDB= "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=jaovo_msg";//����Դ
try
{
Class.forName(JDriver);//�������ݿ����棬���ظ����ַ���������
}catch(ClassNotFoundException e)
{
//e.printStackTrace();
System.out.println("�������ݿ�����ʧ��");
System.exit(0);
}
System.out.println("���ݿ������ɹ�");
String user="sa";
String password="123456";
Connection con = null;
try {
con = DriverManager.getConnection(connectDB,user,password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//�������ݿ����
System.out.println("�������ݿ�ɹ�");
Statement stmt = null;
try {
stmt=con.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//����SQL�������
//---------------------------------------------------------------------------------------//
RandomString s=new RandomString();
ArrayList list=new ArrayList();
long i=0;
while(i!=99){
//list=s.newRandomString();
list=s.newHarderRandomString();
i++;
System.out.println("-----------------------------------------");
System.out.println("��ţ�"+i);
stmt.executeUpdate("insert into Table2(expression,result)values('"+RandomString.toString(list)+"','"+CheckAnswer.calaulateAnswer(list)+"')");
System.out.println("����ʽ�ӣ�"+RandomString.toString(list));
System.out.println(CheckAnswer.calaulateAnswer(list));
System.out.println("-----------------------------------------");
}}
}
程序运行截图: