一、撰写一篇博客要求讲述四则运算2的设计思想,源程序代码、运行结果截图、编程总结分析,并按照PSP0级的要求记录开发过程中的时间记录日志。
1、设计思想:
①创建test.jsp建立第一个前端界面,提示用户输入要生成的题目数。用户输入数值后,点击提交跳转到testmain.jsp界面,显示生成的题目以及用来输入的文本框,在每道题目输入完答案之后,点击提交跳转到testscore.jsp界面即评分界面,显示正确和错误并统计正确和错误的数量,错误的题目用红字提示正确答案。
②shizi.java中的shizi类用来生成表达式,number1和number2确定范围1~100之间,运算符0~3随机数对应运算符,在答案result小于零或者除法不能被整除时就重新生成。
③biaodashizu.java用来实现一些关于数据库的增删改查等操作,delete()用来每次生成题目前清空数据库表,create()用来生成,xieru()用来将用户的答案输入数据库表中,panduan()用来对数据库里的result和answer两个属性进行判断,一样则为回答正确,该题目的正确性录入为1,错误的话,题目的正确性录入为2.
④GNUtil.java是连接数据库的工具类。
⑤建立数据库表来存储信息,id代表题号,number1和number2用来存两个数,yunsuanfu用来存储运算符号,result用来存储该表达式的正确答案,answer用来存储用户输入的答案,zhengwu用来存int类型的1或者2,表示题目正确与否。
⑥在输入要生成的题目后,生成相应数目的表达式,先将数据库清空,再将这些式子的每个属性分别输入进数据库,存储下来。再在testmain.jsp中读取并显示出来,用户填写后进行存储和比对,将评分结果显示在testscore.jsp页面上。
⑦在input标签中添加required属性用来放置部分文本框不填写内容,使用js插入页面倒计时功能,时间一到自动跳转。
2、源程序代码:
test.jsp
<%@page import="com.gb.yunsuan.biaodashizu" %>
<%@ 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">
<title>题目</title>
</head>
<body background="../picture/background.png" style="background-repeat:no-repeat; background-size:100% 100%; background-attachment:fixed;">
<h2 style="color:blue" align="center">四则运算练习题</h2>
<hr size=%40 color=blue>
<form method="get" action="testmain.jsp">
<table align="center" border="2" width="400" style="background:white">
<tr>
<td>要生成的题目数:</td>
<td>
<input type="text" name="num" required />
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" name="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>
testmain.jsp
<%@page import="com.gb.yunsuan.biaodashizu" %>
<%@ 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">
<title>答题界面</title>
</head>
<body background="../picture/background.png" style="background-repeat:no-repeat; background-size:100% 100%; background-attachment:fixed;">
<h2 style="color:blue" align="center">四则运算练习题</h2>
<hr>
<p align="center" style="color:white">
剩余时间为:
<u><b id="time">30</b>秒</u>
<b><u><a style="color:white" href="javascript:goBack();">返回</a></u></b>
</p>
<br>
<script type="text/javascript">
var sec=document.getElementById("time");
var i=30;
var timer=setInterval(
function()
{
i--;
sec.innerHTML=i;
if(i==1)
{
window.location.href="test.jsp";
} },1000);
function goBack()
{
window.history.go(-1);
}
</script>
<%
biaodashizu biaodashi=new biaodashizu();
if(request.getParameter("num")!=null&&!"".equals(request.getParameter("num").trim()))
{
biaodashi.delete();
int x=Integer.parseInt(request.getParameter("num"));
biaodashi.create(x);
}
%>
<form method="get" action="testscore.jsp">
<table align="center" border="2" width="500" style="background:white">
<tr align="center">
<td>
题目总数为:
</td>
<td colspan="3">
<input type="text" style="width:375px" name="num" value=<%=biaodashi.getSum()%> disabled />
</td>
</tr>
<tr>
<td>题目编号</td>
<td>题目表达式</td>
<td>答案输入</td>
<td>答题情况</td>
<tr>
<%
int x=biaodashi.getSum();
for(int i=0;i<x;i++)
{
%>
<tr>
<td><%=i+1 %></td>
<td><%="\t"+biaodashi.showDan(i).getNumber1()+"\t"+biaodashi.showDan(i).getCh()+"\t"+biaodashi.showDan(i).getNumber2()+"\t"+"=" %></td>
<td>
<input type="text" name="text" required />
</td>
<td>
</td>
</tr>
<%
}
%>
<tr align="center">
<td colspan="4">
<input type="submit" name="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>
testscore.jsp
<%@page import="com.gb.yunsuan.biaodashizu" %>
<%@ 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">
<title>评分界面</title>
</head>
<body background="../picture/background.png" style="background-repeat:no-repeat; background-size:100% 100%; background-attachment:fixed;">
<h2 style="color:blue" align="center">四则运算练习题</h2>
<hr>
<%
biaodashizu biaodashi=new biaodashizu();
String jieshou[]=request.getParameterValues("text");
int TRUE=0,FALSE=0;
for(int i=0;i<biaodashi.getSum();i++)
{
int res=Integer.parseInt(jieshou[i]);
biaodashi.xieru(i+1,res);
biaodashi.panduan(i+1,res);
}
%>
<form method="get" action="testmain.jsp">
<table align="center" border="2" width="500" style="background:white">
<tr>
<td>题目编号</td>
<td>题目表达式</td>
<td>提交答案</td>
<td>答题情况</td>
<tr>
<% for(int i=0;i<biaodashi.getSum();i++)
{
%>
<tr>
<td><%=i+1 %></td>
<td><%="\t"+biaodashi.showDan(i).getNumber1()+"\t"+biaodashi.showDan(i).getCh()+"\t"+biaodashi.showDan(i).getNumber2()+"\t"+"=" %></td>
<td>
<%="\t"+biaodashi.showscore(i).getAnswer() %>
</td>
<td>
<%
if(biaodashi.showscore(i).getStatus()==1)
{
%>
<img src="../picture/正确.png">
<%TRUE++; %>
<%
}
else if(biaodashi.showscore(i).getStatus()==2)
{
%>
<img src="../picture/错误.gif">
<h6 style="color:red">正确答案为:<%=biaodashi.showscore(i).getResult() %></h6>
<%FALSE++; %>
<%
}
%>
</td>
</tr>
<%
}
%>
<tr align="center">
<td colspan="4">
<h2>正确数:<%=TRUE %><%="\t" %>错误数:<%=FALSE %></h2>
</td>
</tr>
<tr align="center">
<td colspan="4">
<a href="test.jsp">重新开始练习</a>
</td>
</tr>
</table>
</form>
</body>
</html>
GBUtil.java
package com.gb.Util; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class GBUtil
{
public static Connection getConnection()
{
try
{
Class.forName("com.mysql.jdbc.Driver"); }
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
String url="jdbc:mysql://localhost:3306/denglu?useSSL=false";
Connection connection=null;
try {
connection=DriverManager.getConnection(url, "root", "242772"); }
catch (SQLException e)
{
e.printStackTrace();
System.out.println("数据库连接失败!");
}
return connection;
}
public static void close(Connection connection)
{
try
{
if(connection!=null)
{
connection.close();
}
}catch(SQLException e)
{
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement)
{
try
{
if(preparedStatement!=null)
{
preparedStatement.close();
}
}catch(SQLException e)
{
e.printStackTrace();
}
}
public static void close(ResultSet resultSet)
{
try
{
if(resultSet!=null)
{
resultSet.close();
}
}catch(SQLException e)
{
e.printStackTrace();
}
} }
shizi.java
package com.gb.yunsuan;
import java.util.Random;
public class shizi
{
int number1;
int number2;
int fu;
String ch;
int result=-1;
int answer;
int status;
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public shizi()
{
Random ran=new Random();
while(result<0||result>100)
{
number1=ran.nextInt(100)+1;
number2=ran.nextInt(100)+1;
fu=ran.nextInt(4);
switch(fu)
{
case 0:
ch="+";
result=number1+number2;
break;
case 1:
ch="-";
result=number1-number2;
break;
case 2:
ch="*";
number1=ran.nextInt(9)+1;
number2=ran.nextInt(9)+1;
result=number1*number2;
break;
case 3:
ch="/";
while(number1%number2!=0)
{
number1=ran.nextInt(100)+1;
number2=ran.nextInt(100)+1;
}
result=number1/number2;
break;
}
}
}
public int getNumber1() {
return number1;
}
public void setNumber1(int number1) {
this.number1 = number1;
}
public int getNumber2() {
return number2;
}
public void setNumber2(int number2) {
this.number2 = number2;
}
public String getCh() {
return ch;
}
public void setCh(String ch) {
this.ch = ch;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
} }
biaodashizu.java
package com.gb.yunsuan;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import com.gb.Util.GBUtil;
import com.gb.yunsuan.shizi;
public class biaodashizu
{
public void create(int j)//生成
{
Connection connection=GBUtil.getConnection();
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
for(int i=0;i<j;i++)
{
shizi s=new shizi();
String sql="insert into sizeyunsuan(id,number1,ch,number2,result) value(?,?,?,?,?)";
try
{
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, i+1);
preparedStatement.setInt(2, s.number1);
preparedStatement.setString(3, s.ch);
preparedStatement.setInt(4, s.number2);
preparedStatement.setString(5, ""+s.result);
preparedStatement.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
GBUtil.close(connection);
GBUtil.close(preparedStatement);
GBUtil.close(resultSet);
} public void delete()//清空
{
Connection connection=GBUtil.getConnection();
String sql="delete from sizeyunsuan";
PreparedStatement preparedStatement=null;
try
{
preparedStatement=connection.prepareStatement(sql);
preparedStatement.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
GBUtil.close(connection);
GBUtil.close(preparedStatement);
}
}
public shizi showDan(int i)//返回单个
{
Connection connection=GBUtil.getConnection();
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
String sql="select * from sizeyunsuan where id=?";
shizi shi=null;
try
{
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, i+1);
resultSet=preparedStatement.executeQuery();
while(resultSet.next())
{
shi=new shizi();
shi.setNumber1(resultSet.getInt("number1"));
shi.setCh(resultSet.getString("ch"));
shi.setNumber2(resultSet.getInt("number2"));
shi.setResult(resultSet.getInt("result"));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
GBUtil.close(connection);
GBUtil.close(preparedStatement);
GBUtil.close(resultSet);
return shi;
}
public shizi showscore(int i)//返回评分界面的
{
Connection connection=GBUtil.getConnection();
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
String sql="select * from sizeyunsuan where id=?";
shizi shi=null;
try
{
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, i+1);
resultSet=preparedStatement.executeQuery();
while(resultSet.next())
{
shi=new shizi();
shi.setNumber1(resultSet.getInt("number1"));
shi.setCh(resultSet.getString("ch"));
shi.setNumber2(resultSet.getInt("number2"));
shi.setResult(resultSet.getInt("result"));
shi.setAnswer(resultSet.getInt("answer"));
shi.setStatus(resultSet.getInt("zhengwu"));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
GBUtil.close(connection);
GBUtil.close(preparedStatement);
GBUtil.close(resultSet);
return shi;
}
public int getSum()
{
int n=0;
Connection connection=GBUtil.getConnection();
String sql="select id from sizeyunsuan";
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try
{
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
while(resultSet.next())
{
n++;
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return n;
}
public void xieru(int j,int result)//将用户答案写入数据库
{
Connection connection=GBUtil.getConnection();
PreparedStatement preparedStatement=null;
String sql="update sizeyunsuan set answer = ? where id = ?";
try {
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, result);
preparedStatement.setInt(2, j);
preparedStatement.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
GBUtil.close(connection);
GBUtil.close(preparedStatement);
}
}
public void panduan(int j,int result)//判断正误
{
Connection connection=GBUtil.getConnection();
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
String sql="select * from sizeyunsuan where id=?";
shizi shi=null;
try
{
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, j);
resultSet=preparedStatement.executeQuery();
while(resultSet.next())
{
shi=new shizi();
shi.setNumber1(resultSet.getInt("number1"));
shi.setCh(resultSet.getString("ch"));
shi.setNumber2(resultSet.getInt("number2"));
shi.setResult(resultSet.getInt("result"));
shi.setAnswer(resultSet.getInt("answer"));
shi.setStatus(0);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
if(shi.getResult()!=result)
{
shi.setStatus(2);
}
else
{
shi.setStatus(1);
}
sql="update sizeyunsuan set zhengwu = ? where id = ?";
try
{
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, shi.getStatus());
preparedStatement.setInt(2, j);
preparedStatement.executeUpdate();
}
catch(SQLException e)
{
e.printStackTrace();
}
GBUtil.close(connection);
GBUtil.close(preparedStatement);
GBUtil.close(resultSet);
} }
3、运行结果截图:
初始界面,用来设定数目:
答题界面:
评分界面:
编程总结分析:
这个题目在加入数据库的运用后,就变得条理清晰了,通过工具类连接使用数据库,通过实例化对象调用函数对数据库中数据进行操作。jsp的一些前端布置也很重要,一些常用的功能需要使用js来实现。这个题针对二年级小学生,所以那游戏海报来作为背景图,更能吸引用户。
时间记录日志:
学生:郭斌
日期:2017/12/7
教员:王建民
课程:软件单元测试与代码规范
个人收获:
在这个四则运算的个人项目中,我学会了布局规划,充分利用好数据库,这样会很方便,除此之外,学会了用js完成倒计时页面跳转,收获还算可以。对于必填项的设置,使用input标签中的required属性即可。