四则运算2
设计思想
1、用户设置时间和题数量
2、系统随机产生加减乘除运算,并判断结果是否在100以内以及结果是否是整数
3、用户输入,在系统中和数据库中进行比较,看结果是否正确
4、输出题目以及结果,得到成绩
<%@ 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> <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> <img alt="嘻嘻" src="E:\eclipse\User_Math\timg.jpg" style="width: 510px; height: 275px;" /> </center> </body> </html>
<%@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()"> -->
<%@page import="java.util.List"%> <%@page import="com.Dao.MathDao"%> <%@page import="com.math.UserMath"%> <%@page import="java.util.Enumeration"%> <%@page import="org.apache.taglibs.standard.lang.jstl.EnumeratedMap"%> <%@ 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>
package com.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 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; } }
<%@ 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> <script language="javascript"> alert("时间结束!"); </script> </body> </html>
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(); } } }
package com.math; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Random; import com.Dao.MathDao; //随机产生数学题 //写到数据库中 public class NewMath { public static void main(String[] args) { randomMath(); } public static void randomMath() { 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","27",}; String[] two= new String[] {"+","-","*","/"}; Random random = new Random(); PrintWriter out = null; String sql = null; try { out = new PrintWriter("D:\\eclipse-workspace02\\sql\\insert.sql"); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } for(int i = 0;i<100;i++) { int onenum=random.nextInt(one.length); int twonum=random.nextInt(one.length); int n=(int)(0+Math.random()*4); int number=0; String c1=two[n]; String str=null; switch(c1) { case "+": number=onenum+twonum+2;break; case "-": number=onenum-twonum;break; case "*": number=(onenum+1)*(twonum+1); if(number>=100) { number = -100; } break; case "/": number=(onenum+1)/(twonum+1); if((onenum+1)%(twonum+1)!=0) { number = -100; } break; } if(number>=0) { MathDao mathDao = new MathDao(); UserMath userMath = new UserMath(); str=one[onenum]+two[n]+one[twonum]; userMath.setMathname(str); userMath.setMathanswer(number); mathDao.add(userMath); sql = "insert into t_user (\""+str+"\"="+number+")"; out.println(sql); out.flush(); } else { str="超出范围"; sql = "insert into t_user (\""+str+"\"="+number+")"; out.println(sql); out.flush(); } } } }
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; } }