软件工程结对作业01
设计思想:首先就是设计一个欢迎界面,在这里可以设置要进行的题目个数和时间,然后跳转到下个界面,在下个界面进行接收输入的时间和个数,用时间的方法,来随时体现时间的变化,随机数产生的题目和答案村到数据库中,然后就是在输入框供用户进行输入,获取输入的值与数据库中进行相对应的匹配,判断是否相等,不相等的答案和题目标号存到数组中然后就是可以再下个界面进行展示,供用户进行观看。、
原程序代码:
(1)MathDao.java
package com.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.DBUtil.DBUtil;
import com.math.UserMath;
//import com.mysql.jdbc.Statement;
public class MathDao {
public void add(UserMath userMath) {
PreparedStatement preparedStatement = null;
// ResultSet resultSet = null;
Connection connection = DBUtil.getConnection();
String sql = "insert into t_math(mathname,mathanswer) value(?,?)";
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,userMath.getMathname());
preparedStatement.setInt(2,userMath.getMathanswer());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
}
public void deleteMath() {
Connection connection = DBUtil.getConnection();
Statement statement=null;
try{
statement = (Statement) connection.createStatement();
statement.executeUpdate("delete from t_math");
}catch(SQLException e){
e.printStackTrace();
}finally {
DBUtil.close(connection);
}
}
public List<UserMath> load() {
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from t_math ";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//集合中只能放入user对象
List<UserMath> userMaths = new ArrayList<UserMath>();
UserMath userMath = null;
try {
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
userMath = new UserMath();
userMath.setId(resultSet.getInt("id"));
userMath.setMathname(resultSet.getString("mathname"));
userMath.setMathanswer(resultSet.getInt("mathanswer"));
userMaths.add(userMath);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return userMaths;
}
}
(2)DBUtil.java
package com.DBUtil;
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.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String user="root";
String password="root";
String url="jdbc:mysql://localhost:3306/math";
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();
}
}
}
(3)usermath.Java
package com.math;
public class UserMath {
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
String mathname;
public String getMathname() {
return mathname;
}
public void setMathname(String mathname) {
this.mathname = mathname;
}
int mathanswer;
public int getMathanswer() {
return mathanswer;
}
public void setMathanswer(int mathanswer) {
this.mathanswer = mathanswer;
}
}
(4)Action.jsp
<%@ 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>
<title>开始页面</title>
</head>
<body bgcolor="#E4EEF9">
<div style="color:blue ; text-align: center ">
欢迎使用该系统
</div>
<hr width="100%" color="blue" />
<center>
<form action="Study.jsp" method="get">
<table>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>请设置时间</td>
<td><input type="text" name="time"/></td>
<td>(分钟)</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>设置题数量</td>
<td><input type="text" name="number"/></td>
<td>(个)</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="开始"/>
<input type="reset" value="重新设置"/>
</td>
<td></td>
</tr>
</table>
</form>
</center>
<center>
<br>
<br>
<%String qwe="timg.jpg" ;%>
<img alt="嘻嘻" src=<%=qwe %> width: 510px; height: 275px;" />
</center>
</body>
</html>
(5)examine.jsp
<%@page import="java.util.List"%>
<%@page import="com.Dao.MathDao"%>
<%@page import="com.math.UserMath"%>
<%@page import="java.util.Enumeration"%>
<%@ 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 math[]=request.getParameterValues("mathanswer");
%>
<%
int N=0;
int M=0;
MathDao mathDao = new MathDao();
List<UserMath> userMaths = mathDao.load();
%>
<html>
<head>
<title>检查页面</title>
</head>
<body>
<%
int i = 0;
%>
<td >我们来看看正确答案吧:</td>
<%
int wrong[]=new int[100];//存错题答案的数组
int wrongnumber[]=new int[100];//存错题答案序号的数组
for(UserMath userMath : userMaths){
System.out.println(userMath.getMathanswer());
if(Integer.parseInt(math[i]) == userMath.getMathanswer()){
N++;
}
else
//判断做错的题
{
wrongnumber[M]=i+1;
wrong[M]=Integer.parseInt(math[i]);//村错题
M++;
}
%>
<table>
<tr>
<tr>
<td >第<%=i+1 %>道题是</td>
<td>
<%=userMath.getMathname()
//调取数据库中的题目名称
%>
<td></td>
</td>
<td>
<%=userMath.getMathanswer()
//调取数据库中的答案
%>
</td>
</tr>
</table>
<%
i++;
}
%>
<table>
<tr>看看我们做错的题:</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<%for(int z=0;z<M;z++)
{
%>
<tr>
<td>做错的第<%= wrongnumber[z]%>道题答案</td>
<td></td>
<td><%=wrong[z] %></td>
</tr>
<%
}
%>
<tr>
<td >我们作对了:</td>
<td><%=N%></td>
<td>道题,加油哦!你是最棒的</td>
</tr>
</table>
</body>
</html>
(6)study.jsp
<%@page import="com.math.UserMath"%>
<%@page import="com.Dao.MathDao"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.util.Random"%>
<%@ 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">
<%
int time = Integer.parseInt(request.getParameter("time"));
int number = Integer.parseInt(request.getParameter("number"));
%>
<html>
<head>
<title>题目页面</title>
<!-- 10秒内自动调转到 url页面 -->
<meta http-equiv="refresh" content="<%=time*60+1 %>;url=timeOut.jsp">
</script>
<script language="JavaScript">
//获取当前时间
startday=new Date();
clockStart=startday.getTime();
function initStopwatch(){
var myTime=new Date();
var timeNow=myTime.getTime();
var timeDiff=timeNow-clockStart;//获取间隔时间
this.diffSecs=timeDiff/1000; //时间以毫秒为单位
return(this.diffSecs);
}
function getSecs(){
var mySecs=initStopwatch();
var mySecs1=""+mySecs;
var time = parseInt(mySecs1);
var h = (time/3600);
var h1 = ""+h;
var h2 = parseInt(h1);
var m = ((time-3600*h2)/60);
var m1 = ""+m;
var m2 = parseInt(m1);
var s = (time-3600*h2-60*m2);
var s1 = ""+s;
var s2 = parseInt(s1);
var t = h2+"时"+m2+"分"+s2+"秒";
document.form1.timespent.value=t;
window.setTimeout('getSecs()',1000);
}
</script>
</head>
<body bgcolor="#E4EEF9" onLoad="window.setTimeout('getSecs()',1)">
<br>
<center>
<form action="Examine.jsp">
<table>
<tr>
<td>
<h4 style="color: blue">设置的时间为:<%=time %>分钟 </h4>
</td>
</tr>
</table>
</form>
<br>
<form name=form1> <input size=25 name=timespent></form><br>
<hr width="100%" color="blue" />
</center>
<form action="Examine.jsp" method="get">
<table>
<tr>
<td width="250" align="center">题目</td>
<td align="center">请输入答案</td>
<td width="280"></td>
<td width="250" align="center">3</td>
<td align="center">4</td>
<td></td>
</tr>
<%
UserMath userMath = new UserMath();
MathDao mathDao = new MathDao();
mathDao.deleteMath();
String[] one= new String[] {"1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18",
"19","20","21","22","23","24","25","26",
"27","28","29","30","31","32","33",
"34","35","36","37","38","39","40","41",
"42","43","44","45","46","47","48","49",
"50","51","52","53","54","55","56","57",
};
String[] two= new String[] {"+","-","*","/"};
Random random = new Random();
//PrintWriter out = null;
String sql = null;
%>
<%
//储存用户输入信息
//int mathasnwer[] = new int[number];
int number1=0;
int n=0;
int truemath=0;
//int pas[]=new int[5];
// int i=0;
%>
<%
do{
int onenum=random.nextInt(one.length);
int twonum=random.nextInt(one.length);
int n1=(int)(0+Math.random()*4);
//number是结果
String c1=two[n1];
//str是题目
String str1=null;
switch(c1) {
case "+":
number1=onenum+twonum+2;
if(number1>=100) {
number1 = -100;
}
break;
case "-":
number1=onenum-twonum;
if(number1>=100) {
number1 = -100;
}
break;
case "*":
number1=(onenum+1)*(twonum+1);
if(number1>=100) {
number1 = -100;
}
break;
case "/":
number1=(onenum+1)/(twonum+1);
if((onenum+1)%(twonum+1)!=0) {
number1 = -100;
}
break;
}
if(number1>=0) {
n++;//1
str1=one[onenum]+two[n1]+one[twonum];
userMath.setMathname(str1);
userMath.setMathanswer(number1);
mathDao.add(userMath);
%>
<tr>
<td width="250" align="center"><%=str1 %>=</td>
<td><input type="text" name="mathanswer"/></td>
<td width="280"></td>
<td width="250" align="center">√</td>
<td align="center">4</td>
<td></td>
</tr>
<%
}
}while(n<number);
%>
<tr>
<td>
<input type="submit" value="完成提交"/>
</td>
</tr>
</table>
</form>
</body>
</html>
<!--
<html lang="zh-CN">
<meta charset="utf-8">
<head>
<title>倒计时</title>
</head>
<script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
<script>
$(function(){
var timerVal = $("#timer").val();
var i = setInterval(function() {
timerVal--;
$("#timer").val(timerVal);
if (timerVal < 1)
clearInterval(i);
}, 1000);
});
</script>
<body>
<center>
倒计时<input id="timer" value="10"/>
</center>
</body>
</html>
<script type="text/javascript">
function welcom() {
alert("欢迎");
}
</script>
<body onload="welcom()">
-->