1.设计思想:
主要是随机生成两个数(根据情况限制一下),做它们加减乘除,将式子与结果存入数据库,用户输入自己的答案,与数据库答案比较,输出做题情况!!
2.源程序代码
/**连接数据库*/
package 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 { Class.forName("com.mysql.jdbc.Driver").newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } String user = "root"; String password = "root"; String url = "jdbc:mysql://localhost:3306/jaovo_msg"; 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(); } } }
/**操作*/
/**11*/ <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%> <!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> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(a.jpg); } </style> </head> <body> <div align="center"> <%! int i=0; String Array[]=new String[1000];//array 表达式 class Zhengshu//整数类 { String re1="";//初始化 “结果” String类型的 int re; String function(int r ,int q, int fun)// r最小值,q最大值,fun 选择 { //1有乘除法 2加减无负数 3除法无余数(12,1,0) String fu =""; String shizi=""; int a=(int)(Math.random()*(r)%(r-q+1)+q); int b=(int)(Math.random()*(r)%(r-q+1)+q); int c=(int)(Math.random()*4); if(fun==3) { if(c==0) { fu="+"; re=a+b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a-b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==2) { fu="+"; re=a+b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==3) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a-b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } } //2有乘除法余数不定 if(fun==2) { if(c==0) { fu="+"; re=a+b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } shizi=a+" "+fu+" "+b; re=a-b; re1=String.valueOf(re); } if(c==2) { fu="×"; re=a*b; re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } if(c==3) { fu="÷"; //排除分母为0的可能性 while(b==0) { b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a/b; if(a%b!=0) { int flag1=a%b; re1=String.valueOf(re)+"..."+String.valueOf(flag1); } else re1=String.valueOf(re); shizi=a+" "+fu+" "+b; } } //1有乘除法 if(fun==1) { if(c==0) { fu="+"; shizi=a+" "+fu+" "+b; re=a+b; re1=String.valueOf(re); } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } shizi=a+" "+fu+" "+b; re=a-b; re1=String.valueOf(re); } if(c==2) { fu="×"; shizi=a+" "+fu+" "+b; re=a*b; re1=String.valueOf(re); } if(c==3) { fu="÷"; //排除分母为0的可能性和余数的可能性 while(b==0||a%b!=0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a/b; shizi=a+" "+fu+" "+b; re1=String.valueOf(re); } } return shizi;//返回字符串 } } %> <% Connection con = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jaovo_msg", "root", "root"); //链接本地MYSQL java.sql.Statement stmt; //创建声明 stmt = con.createStatement();//创建传输对象 stmt.executeUpdate("delete from 四则运算题目2"); int max=Integer.parseInt(String.valueOf(session.getAttribute("max")));//转换max为int int min=Integer.parseInt(String.valueOf(session.getAttribute("min")));//转换min为int int choose=Integer.parseInt(String.valueOf(session.getAttribute("choose")));//转choose int geshu=Integer.parseInt(String.valueOf(session.getAttribute("geshu")));//转换geshu session.setAttribute("geshu1", geshu);//获得session的值 //String daan=request.getParameter("daan"); int zhengque1=0;//对答案正确性作标记 int cuowu1=0;//对答案错误作标记 /** 疑问: *表示正确的数组的长度是个数+10 *表示错误的数组的长度是个数+10 */ int zhengque4[]=new int[geshu+10];//? int cuowu4[]=new int[geshu+10]; Zhengshu a = new Zhengshu();//整数类制造对象,可以返回表达试 //for(int i=0;i<geshu;i++) %> <% //if(i<geshu)//i的初值为零,满足条件 for(int j=0;j<geshu;j++) { Array[i]=a.function(max,min,choose);//返回第一个字符串(字符串) if(i>0)//不满足条件,i的初值为零 { for(int z=0;z<i;z++) { if((Array[z].equals(Array[i]))) { Array[i]=a.function(max,min,choose); z=-1; } } } out.println(i+1+": "+Array[i]+" = "+' '); //输出i+1表示第几道题;输出表达式加上“=” 1+1 = %> <% //将题目和答案写入数据库表 String sql="INSERT INTO 四则运算题目2 (shizi,result) VALUES ('"+Array[i]+" = "+"','"+a.re1+"')"; //out.println("<script language = 'javaScript'> alert('"+shi+"');</script>"); stmt.executeUpdate(sql);//执行sql语句、 i++; } response.setHeader("refresh","0;url =disanjiemian.jsp"); %> <br> </div> <form id="form1" name="form1" method="post" action="disanjiemian.jsp"> <p> </p> <p align="center">答案: <input type="text" name="daan" id="daan" /> <% String shi3=Array[i]+" = "; session.setAttribute("shi",shi3); String rere3=a.re1; session.setAttribute("rere",rere3); //String daan3=request.getParameter("daan"); //session.setAttribute("daan",daan3); session.setAttribute("i2",i); //i++; stmt.close(); con.close(); /** *把第一个式子放到数据库,然后在zhengshusjk.jsp中比较结果 *array不包括“=” *输入答案 *此时已将array存入数据库 *再把array+“=”存入shi字符串 *把shi放到session里 *String型的结果放到rere3 *把rere3放到session里 *把i放到session里 *i+1 *提交 */ %> <input type="submit" value="下一个" > </p> </form> <div align="center"> <% //String daan3=request.getParameter("daan"); //daan3=request.getParameter("daan"); //session.setAttribute("daan",daan3); %> } <%/* } else { i=0; */%> </div> <%/* <form action="chakan.jsp" method="post" >*/%> <div align="center"> <% // String correct=String.valueOf(session.getAttribute("correct1")); // String wrong=String.valueOf(session.getAttribute("wrong1")); %> <% /*<input type="submit" value="查看成绩" name="button" align="middle"> */%> </div> </form> <%/* <div align="center"> */%> <%/* } */%> </div> </body> </html> /**chakan*/ <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <style type="text/css"> body,td,th { font-size: 24px; } body { background-image:url("../images/5.jpg"); background-repeat:no-repeat; background-size:130% auto; } </style> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>整数式子</title> </head> <body> <table align="center" border="1" width="600" style=" 624px;"> <tr> <td width="250"><%="式子"%></td> <td width="150"><%="答案"%></td> <td width="150"><%="正确答案"%></td> <td width="50"><%="结果"%></td> </tr> </table> <% Connection con = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jaovo_msg", "root", "root"); //链接本地MYSQL java.sql.Statement stmt; //创建声明 stmt = con.createStatement();//创建传输对象 <br> String sql2 = "select result from 四则运算题目3"; ResultSet resultSet = null; resultSet= stmt.executeQuery(sql2); String anw[]=new String[1000]; int p=0; while(resultSet.next()){ anw[p]=resultSet.getString("result"); p++; } String an[]=request.getParameterValues("anwser"); Connection conn = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jaovo_msg", "root", "root"); //链接本地MYSQL java.sql.Statement stmtn; //创建声明 stmtn = conn.createStatement();//创建传输对象 <br> String sql3 = "select shizi from 四则运算题目3"; ResultSet resultSetn = null; resultSetn= stmtn.executeQuery(sql3); int i=0; int jud1=0;//判断对的式子个数 int jud2=0;//判断错的式子个数 while(resultSetn.next()){ String anwser=""; String ar; String jud; ar=resultSetn.getString("shizi"); if(an[i].equals(anw[i])) { jud="√"; jud1++; } else{ jud="×"; jud2++; } %> <table align="center" border="1" width="600" style=" 624px; "> <tr> <td width="250"><%=ar %></td> <td width="150"><%=an[i]%></td> <td width="150"><%=anw[i]%></td> <td width="50"><%=jud%></td> </tr> </table> <% i++; } %> <font face="楷体_GB2312" color="blue" size="+3">你一共对了<a><%=jud1 %></a>道题!!!</font><br> </body> </html> /**choose*/ <%@ page contentType = "text/html; charset=utf-8" import = "java.sql.*" errorPage = "error.jsp" %><%//java.sql %> <!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> <style> body { background: #222; margin: auto; 960px; } .arc-text { font: 16px sans-serif; } .arc-center { fill: none; } #credit { position: absolute; font: 10px sans-serif; right: 10px; bottom: 10px; color: #ddd; } #credit a { color: inherit; } </style> <div id="credit">Inspired by <a href="http://blog.pixelbreaker.com/polarclock/">pixelbreaker</a>.</div> <script src="//d3js.org/d3.v3.min.js"></script> <script> var width = 960, height = 800, radius = Math.min(width, height) / 1.9, spacing = .09; var formatSecond = d3.time.format("%-S seconds"), formatMinute = d3.time.format("%-M minutes"), formatHour = d3.time.format("%-H hours"), formatDay = d3.time.format("%A"), formatDate = function(d) { d = d.getDate(); switch (10 <= d && d <= 19 ? 10 : d % 10) { case 1: d += "st"; break; case 2: d += "nd"; break; case 3: d += "rd"; break; default: d += "th"; break; } return d; }, formatMonth = d3.time.format("%B"); var color = d3.scale.linear() .range(["hsl(-180,60%,50%)", "hsl(180,60%,50%)"]) .interpolate(function(a, b) { var i = d3.interpolateString(a, b); return function(t) { return d3.hsl(i(t)); }; }); var arcBody = d3.svg.arc() .startAngle(0) .endAngle(function(d) { return d.value * 2 * Math.PI; }) .innerRadius(function(d) { return d.index * radius; }) .outerRadius(function(d) { return (d.index + spacing) * radius; }) .cornerRadius(6); var arcCenter = d3.svg.arc() .startAngle(0) .endAngle(function(d) { return d.value * 2 * Math.PI; }) .innerRadius(function(d) { return (d.index + spacing / 2) * radius; }) .outerRadius(function(d) { return (d.index + spacing / 2) * radius; }); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); var field = svg.selectAll("g") .data(fields) .enter().append("g"); field.append("path") .attr("class", "arc-body"); field.append("path") .attr("id", function(d, i) { return "arc-center-" + i; }) .attr("class", "arc-center"); field.append("text") .attr("dy", ".35em") .attr("dx", ".75em") .style("text-anchor", "start") .append("textPath") .attr("startOffset", "50%") .attr("class", "arc-text") .attr("xlink:href", function(d, i) { return "#arc-center-" + i; }); tick(); d3.select(self.frameElement).style("height", height + "px"); function tick() { if (!document.hidden) field .each(function(d) { this._value = d.value; }) .data(fields) .each(function(d) { d.previousValue = this._value; }) .transition() .ease("elastic") .duration(500) .each(fieldTransition); setTimeout(tick, 1000 - Date.now() % 1000); } function fieldTransition() { var field = d3.select(this).transition(); field.select(".arc-body") .attrTween("d", arcTween(arcBody)) .style("fill", function(d) { return color(d.value); }); field.select(".arc-center") .attrTween("d", arcTween(arcCenter)); field.select(".arc-text") .text(function(d) { return d.text; }); } function arcTween(arc) { return function(d) { var i = d3.interpolateNumber(d.previousValue, d.value); return function(t) { d.value = i(t); return arc(d); }; }; } function fields() { var now = new Date; return [ {index: .7, text: formatSecond(now), value: now.getSeconds() / 60}, {index: .6, text: formatMinute(now), value: now.getMinutes() / 60}, {index: .5, text: formatHour(now), value: now.getHours() / 24}, {index: .3, text: formatDay(now), value: now.getDay() / 7}, {index: .2, text: formatDate(now), value: (now.getDate() - 1) / (32 - new Date(now.getYear(), now.getMonth(), 32).getDate())}, {index: .1, text: formatMonth(now), value: now.getMonth() / 12} ]; } </script> <style type="text/css"> body,td,th { font-size: 24px; } body { background-image:url("../images/0.gif"); background-repeat:no-repeat; background-size:100% 100%; } <style> <div { background-image:url("../images/0.gif"); background-repeat:no-repeat; background-size:100% 100%; } </style> </style> </head> <body> <form id="form1" name="form1" method="post" action="zhengshu.jsp"> <% /*<form id="myForm" name="myForm"*>*/%> <div align="center"> <input type="submit" value="开始答题" onClick="zhengshu.jsp"></p><br><br> </div> </form> <script> function check(){ var input = document.getElementsByName("RadioGroup1"); for(var i=0; i< input.length; i++ ){ if(input[i].checked == true ){ //window.open(input[i].value,null); //新窗口打开 window.location=input[i].value; //当前窗口打开,注释一种方法 } } } </script> </body> </html> /**disanjiemian*/ <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <style type="text/css"> body,td,th { font-size: 24px; } body { background-image:url("../images/4.jpg"); background-repeat:no-repeat; background-size:130% auto; } </style> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>整数式子</title> </head> <body> <form action="chakan.jsp" method="post"> <% Connection con = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jaovo_msg", "root", "root"); //链接本地MYSQL java.sql.Statement stmt; //创建声明 stmt = con.createStatement();//创建传输对象 <br> String sql2 = "select shizi from 四则运算题目3"; ResultSet resultSet = null; resultSet= stmt.executeQuery(sql2); //String array[]=new String[1000]; %> <table align="center" border="1"width="600" style=" 624px;"> <tr> <td width="250"><%="式子"%></td> <td width="150"><%="答案"%></td> <td width="150"><%="正确答案"%></td> <td width="50"><%="结果"%></td> </tr> </table> <% while(resultSet.next()){ String anwser=""; int i=0; String ar; ar=resultSet.getString("shizi"); i++; %> <table align="center" border="1" width="600" style=" 624px;"> <tr> <td width="250"><%=ar %></td> <td width="150"><input type="text" name="anwser" autocomplete="off" /></td> <td width="150"></td> <td width="50"></td> </tr> </table> <% } %> <tr align="center"> <td colspan="2"> </td> </tr> <div align="center"> <input type="submit" value="提交" style="height: 41px; 81px; "></div> </body> </html> /**zhengshu*/、 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <style type="text/css"> body,td,th { font-size: 24px; } body { background-image:url("../images/1.gif"); background-repeat:no-repeat; background-size:auto auto; } </style> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>整数</title> </head> <body style=" 1333px; "> <form id="form1" name="form1" method="post" action="zhengshushizi.jsp" style="height: 469px; 1353px; "> <p> <label for="textfield"></label> </p> <p> </p> <p> </p> <p> </p> <p align="center" style=" 1242px; height: 78px"><br> 题目个数:<input type="text" name="geshu" autocomplete="off" id="geshu"> <br> </p> <p> </p> <div align="center"> <input type="submit" value="确定" value="zhengshushizi.jsp"> </div> </form> </body> </html> /**zhengshushizi*/ <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.sql.Connection,java.sql.DriverManager,java.sql.ResultSet"%> <!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> <style type="text/css"> body,td,th { font-size: 24px; } body { background-color: #0CF; background-image: url(a.jpg); } </style> </head> <body> <div align="center"> <%! int i=0; String Array[]=new String[1000];//array 表达式 class Zhengshu//整数类 { String re1="";//初始化 “结果” String类型的 int re; String function(int r ,int q, int fun)// r最小值,q最大值,fun 选择 { //1有乘除法 2加减无负数 3除法无余数(12,1,0) String fu =""; String shizi=""; int a=(int)(Math.random()*(r)%(r-q+1)+q); int b=(int)(Math.random()*(r)%(r-q+1)+q); int c=(int)(Math.random()*4); if(fun==1) { if(c==0) { while(a+b>100){ a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } fu="+"; shizi=a+" "+fu+" "+b; re=a+b; re1=String.valueOf(re); } if(c==1) { fu="-"; //排除负数可能性 while(a-b<0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } shizi=a+" "+fu+" "+b; re=a-b; re1=String.valueOf(re); } if(c==2) { while(a>10||b>10){ a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } fu="×"; shizi=a+" "+fu+" "+b; re=a*b; re1=String.valueOf(re); } if(c==3) { while(a>10||b>10){ a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } fu="÷"; //排除分母为0的可能性和余数的可能性 while(b==0||a==0) { a=(int)(Math.random()*(r)%(r-q+1)+q); b=(int)(Math.random()*(r)%(r-q+1)+q); } re=a; shizi=a*b+" "+fu+" "+b; re1=String.valueOf(re); } } return shizi;//返回字符串 } } %> <% Connection con = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jaovo_msg", "root", "root"); //链接本地MYSQL java.sql.Statement stmt; //创建声明 stmt = con.createStatement();//创建传输对象 stmt.executeUpdate("delete from 四则运算题目3"); // stmt.executeUpdate("delete from 四则运算题目2"); /* int max=Integer.parseInt(String.valueOf(session.getAttribute("max")));//转换max为int int min=Integer.parseInt(String.valueOf(session.getAttribute("min")));//转换min为int int choose=Integer.parseInt(String.valueOf(session.getAttribute("choose")));//转choose*/ /*int geshu=Integer.parseInt(String.valueOf(session.getAttribute("geshu")));//转换geshu session.setAttribute("geshu1", geshu);//获得session的值*/ //String daan=request.getParameter("daan"); /* int zhengque1=0;//对答案正确性作标记 int cuowu1=0;//对答案错误作标记*/ /** 疑问: *表示正确的数组的长度是个数+10 *表示错误的数组的长度是个数+10 */ int geshu=Integer.parseInt(request.getParameter("geshu")); int zhengque4[]=new int[geshu+10];//? int cuowu4[]=new int[geshu+10]; Zhengshu a = new Zhengshu();//整数类制造对象,可以返回表达试 //for(int i=0;i<geshu;i++) %> <% //if(i<geshu)//i的初值为零,满足条件 for(int j=0;j<geshu;j++) { Array[i]=a.function(1,100,1);//返回第一个字符串(字符串) if(i>0)//不满足条件,i的初值为零 { for(int z=0;z<i;z++) { if((Array[z].equals(Array[i]))) { Array[i]=a.function(100,1,1); z=-1; } } } //out.println(i+1+": "+Array[i]+" = "+' '); //输出i+1表示第几道题;输出表达式加上“=” 1+1 = %> <% //将题目和答案写入数据库表 String sql="INSERT INTO 四则运算题目3 (shizi,result) VALUES ('"+Array[i]+" = "+"','"+a.re1+"')"; //out.println("<script language = 'javaScript'> alert('"+shi+"');</script>"); stmt.executeUpdate(sql);//执行sql语句、 i++; } response.setHeader("refresh","0;url =disanjiemian.jsp"); %> <br> </div> stmt.close(); con.close(); </body> </html>
3.运行结果截图
4.编程总结分析
学会了前端设计 前端有点难啊