1,设计思想
(1)在java resourse里定义包和类
(2)在类里定义生成算式,并将算式保存在数据库中的方法
(3)在jsp文件中调用java方法
2,源程序代码
生成算式的方法
public void create() { // TODO Auto-generated method stub Equation equ=new Equation(); Random ran=new Random(); for(int i=1;i<=1000;i++){ Connection con=DBUtil.getConnection(); equ.setFirstNum(ran.nextInt(0+100)); equ.setSecondNum(ran.nextInt(0+100)); equ.setSymbol(equ.s[ran.nextInt(4)]); if(i<10) equ.setId("000"+i); if(i>=10&&i<100) equ.setId("00"+i); if(i>=100&&i<1000) equ.setId("0"+i); if(i==1000) equ.setId("1000"); if(equ.getSymbol()=="+") equ.setResult(equ.getFirstNum()+equ.getSecondNum()); if(equ.getSymbol()=="-") equ.setResult(equ.getFirstNum()-equ.getSecondNum()); if(equ.getSymbol()=="*") equ.setResult(equ.getFirstNum()*equ.getSecondNum()); if(equ.getSymbol()=="/"&&equ.getSecondNum()!=0&&(equ.getFirstNum()%equ.getSecondNum()==0)&&equ.getFirstNum()>=equ.getSecondNum()) equ.setResult(equ.getFirstNum()/equ.getSecondNum()); { if(equ.getResult()<100&&equ.getResult()>=0){ try{ String sql="insert into equation(id,firstNum,symbol,secondNum,result)values('"+equ.getId()+"','"+equ.getFirstNum()+"','"+equ.getSymbol()+"','"+equ.getSecondNum()+"','"+equ.getResult()+"')"; Statement stmt=con.createStatement(); stmt.executeUpdate(sql);//更新 }catch(SQLException e){ e.printStackTrace(); }finally { DBUtil.close(con); } } else i--; } } } public String show(){ Equation equ=new Equation(); Connection con=DBUtil.getConnection(); String str =""; Random ran=new Random(); Statement stmt; try { stmt = con.createStatement(); int id1=ran.nextInt(999)+1; ResultSet rs=stmt.executeQuery("SELECT * FROM equation where id="+id1); while(rs.next()){ str=rs.getString("id")+" "+rs.getString("firstNum")+rs.getString("symbol")+rs.getString("secondNum")+"="; } } catch (SQLException e) { e.printStackTrace(); } return str; }
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> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>四则运算</title> </head> <center> <body background="D:图片动漫20150422H2607_fKHLB.jpeg"> <%if(request.getAttribute("error1")!=null){ out.print("<script language='javaScript'>alert('题数和时间不能为空'); </script>)"); } %> <%if(request.getAttribute("error2")!=null){ out.print("<script language='javaScript'>alert('请正确输入'); </script>)"); } %> <center> <font color="red" size="4" face="楷体">欢迎来到四则运算</font> </center> <form action="caculateInput.jsp" method="post"> <table align="center" > <tr> <td> <font face="楷体">请输入出题数目</font> </td> <td> <input type="text" name="number" placeholder="请输入……"> </td> </tr> <tr> <td> <font face="楷体">请设置做题时间(秒)</font> </td> <td> <input type="text" name="time" placeholder="请输入……"> </td> </tr> <tr align="center"> <td colspan="2"> <input type="submit" value="开始答题"/> <input type="reset" value="重置题目数目"/> </td> </tr> </table> </form> </body> </center> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="com.jaovo.msg.Util.showAnswer"%> <%@page import="com.jaovo.msg.dao.EquationDaoImpl" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% String url = "http://localhost:8080/四则运算2/equation/start.jsp"; //设置跳转的地址 int time=Integer.parseInt(request.getParameter("time")); int number=Integer.parseInt(request.getParameter("number")); EquationDaoImpl equ=new EquationDaoImpl(); String time1=request.getParameter("time"); String number1=request.getParameter("number"); %> <%if("".equals(time1.trim())||"".equals(number1.trim())){ request.setAttribute("error1", "题数和时间不能为空"); %> <jsp:forward page="start.jsp"></jsp:forward> <% } %> <% if(time<0||number<0){ request.setAttribute("error2", "请正确输入"); %> <jsp:forward page="start.jsp"></jsp:forward> <% } %> <html> <head > <title>四则运算</title> <meta http-equiv=refresh content=<%=time %>;url=<%=url %>> </head> <body background="D:图片新建文件夹9252150_130954294310_2.jpg" background-size:cover> <form action="caculate.jsp" method="post"> <table align="center" > <% for(int i=0;i<number;i++){ String str=equ.show(); %> <tr> <td><%=str.substring(6, str.length()) %></td> <td> <input type="text" name="result" placeholder="请输入……" > </td> </tr> <tr> <td> <input type="hidden" name="id" value="<%=str.substring(0,4) %>" > </td> </tr> <% } %> <div align="center"> <h3 style=color:red>答题剩余时间:<span id=jump ></span></h3> <h4><a href="start.jsp" onclick="jumpToIndex()">重新设题</a></h4> </div> <tr align="center"> <td colspan="2"> <input type="submit" value="提交"/> <input type="reset" value="重置"/> </td> </tr> </table> </form> </body> <script> function countDown(secs){ jump.innerText=secs; if(--secs>0) setTimeout("countDown("+secs+" )",1000); if(secs==0) document.forms[0].submit(); } countDown(<%=time%>); function jumpToIndex(){ window.location.href='<%="http://localhost:8080/四则运算2/equation/start.jsp" %>'; } </script> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.util.*" %> <%@page import="com.jaovo.msg.dao.EquationDaoImpl" %> <!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=ISO-8859-1"> <title>计算结果</title> </head> <body background="D:图片新建文件夹9252150_130954294310_2.jpg"> <% String[] result=request.getParameterValues("result"); String[] id=request.getParameterValues("id"); String[] idNum=new String[100]; int I=0; for(String i:id){ idNum[I]=i; I++; } String[] RCaculate=new String[100]; int x=0; String[] WCaculate=new String[100]; int y=0; String[] KCaculate=new String[100]; int z=0; I=0; EquationDaoImpl equDao=new EquationDaoImpl(); for(String str:result){ if(str == null || "".equals(str.trim())){ KCaculate[z]=equDao.show1(idNum[I]);z++;continue; } if(equDao.caculate(str,idNum[I]).equals("正确")){ RCaculate[x]=equDao.show1(idNum[I]);x++; } if(equDao.caculate(str,idNum[I]).equals("错误")){ WCaculate[y]=equDao.show1(idNum[I]);y++; } I++; } out.println("正确的数量:"+x); %> <br/> <% out.print("错误的题目数量:"+y); %> <!-- <a href="wrong.jsp">查看错题</a> --> <br/> <% out.print("未答的题目数量:"+z); %> <!-- <a href="">查看未答的题目</a> --> <br/> <a href="create.jsp">更新题目</a><br/> <a href="start.jsp">重新计算</a><br/> <form action="wrong.jsp" method="post"> <table align="center"> <% for(int i=0;i<y;i++){ %> <tr> <td> <input type="hidden" name="wrong" value="<%=WCaculate[i]%>" > </td> </tr> <%} %> <% for(int i=0;i<z;i++){ %> <tr> <td> <input type="hidden" name="kong" value="<%=KCaculate[i]%>" > </td> </tr> <%} %> <tr> <input type="submit" value="查看错题和未做的题"/> </tr> </table> </form> </body> </html>
3,运行结果截图