一.设计思想:
1)写出一个菜单界面,有两个选项一个是分数,一个是整数。
2)而这两个标签后面则是转向其更详细的菜单,题目数量,有无括号,运算的项数等等详细功能,再点击这两个按钮之后,就会跳转过去。
3)当选择整数运算时,显示出需要的菜单,输入完要求之后,跳转到生成算式的JSP文件,在此文件中首先是用request方法来获取上个页
面中你所需要的功能,然后再强制类型转化来为下面使用,通过随机数生成,分别生成参与运算的数和符号,0代表加法1代表减法3代表乘法4代
表除法。选择有负数和有余数式子生成,选择没有负数或者没有余数的情况下,就需要将随机的数进行运算,符合条件flag=1,不符合条件的flag
=0。定义一个数来表示他的结果,在每个运算符号的if语句中,将结果运算出来,在整个式子都通过前面的用户需求时,将运算数和符号连成一个
字符串,然后再进行查重,如果没有重复则flag1=1,如果有重复flag1=0。当全部满足条件时,在页面中显示出来,并且在式子右侧写一个文本
框用来填写结果。然后设置一个统计按钮,填完结果之后,统计的答题情况,我们是用session方法来保存式子数组和答案数组的。然后跳转到核对界
面进行核对,如果对了提示回答正确,count++,如果错误提示错误。上面是两个数的运算,多个数的运算则是用的先生成两个数的运算,然后在随
机是否生成括号,如果生成然后在随机括号的位置是在这一组上的前面加括号,还是后面加括号。上面就是整数部分的运算。
4)分数部分的运算和整数部分差不多,首先随机生成四个数两个构成一个分数,然后再定义个随机数来表示运算符。然后,分子分母进行比较
,是否是真分数。如果不是此次循环结束,i--重新开始。需要满足的条件基本上和整数差不多,不过需要在jsp中写一个求最大公约数的方法,来进行
化简,在运算的时候先运算然后进行化简。最后数字和符号组成字符串,结果字符串。通过session方法进行保存。跳到核对界面进行核对。多个分数
运算有无括号和整数的原理也一致。最后来说说数据库,在jsp中连接数据库的话和在.java文件中连接有所不同,在加载驱动时,加载驱动语句右面要
加.Instance();才能加载成功。然后在情况下,所满足条件的生成式子。通过SQL增加语句进行写入数据库。
二.程序源代码:
开始界面
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>菜单页面</title></head> <body ><div align="center"><br/><br/><br/><br/><br/><br/><a href="zs.jsp" >●整数运算</a><br/><br/><a href="fens.jsp">⊙分数运算</a></div> </body> </html>
整数菜单
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>整数二级菜单页面</title> 8 </head> 9 <body> 10 <form name="form1" method=post action="index.jsp"> 11 <center> 整数四则运算</center> 12 <table align="center"> 13 <tr> 14 <td>请输入需要生成的题目数量</td> 15 <td><input name="shuliang" id="shuliang" type="text" > </td> 16 </tr> 17 <tr> 18 <td> 19 请输入最小值 20 </td> 21 <td> 22 <input name="zuixiao" id="zuixiao" type="text"> 23 </td> 24 </tr> 25 <tr> 26 <td>请输入最大值</td> 27 <td> <input name="zuida" id="zuida" type="text"> </td> 28 </tr> 29 <tr> 30 <td> 请选择是否需要括号1有2没有</td> 31 <td> <input name="kuohao" id="kuohao" type="text"> </td> 32 </tr> 33 <tr> 34 <td>请选择生成算式的位数1两位数2多位数</td> 35 <td><input name="weishu" id="weishu" type="text"> </td> 36 </tr> 37 <tr> 38 <td>加减有无负数</td> 39 <td><input name="fs" id="fs" type="text"> </td> 40 </tr> 41 <tr> 42 <td> 除法有无余数</td> 43 <td><input name="ys" id="ys"type="text"></td> 44 </tr> 45 </table> 46 <center><input type="submit" value="开始出题"> </center> 47 </form> 48 </body> 49 </html>
整数试题和填写
<%@ 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> </head> <body> <%String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";Class.forName(driverStr).newInstance();String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=Webys";String dbusername = "sa";String dbpassword = "123456";Connection conn=null;java.sql.Statement stmt=null;conn=DriverManager.getConnection(connStr, dbusername, dbpassword);stmt=conn.createStatement();String weishu=request.getParameter("weishu");String kuohao=request.getParameter("kuohao");String zuix=request.getParameter("zuixiao");String zuid=request.getParameter("zuida");String fss=request.getParameter("fs");String yss=request.getParameter("ys");if(Integer.parseInt(weishu)==1){int ans=0;String j=request.getParameter("shuliang");int sl=Integer.valueOf(j);int litt=Integer.valueOf(zuix);int bi=Integer.valueOf(zuid);String []ss=new String[sl];String s="";String daan[] =new String [sl];String ans2[]=new String[sl];int a[]=new int [sl];int b[]=new int [sl];int flag=0;int flag1=0; int c[]=new int [sl];int d[]=new int[sl];int e[]=new int[sl];int fs=Integer.valueOf(fss);int ys=Integer.valueOf(yss);for(int i=0;i<sl;i++){daan[i]="name"+i;flag1=0;a[i]=(int)(Math.random()*(bi-litt)+1+litt);b[i]=(int)(Math.random()*(bi-litt)+1+litt);c[i]=(int)(Math.floor((Math.random()*4)));if(fs==1){flag=0;}if(fs==2){if(c[i]==1){if((a[i]-b[i])>=0){flag=0;}else{flag=1;}}}if(c[i]==0){s=" + ";ans=a[i]+b[i];}if(c[i]==1){s=" - ";ans=a[i]-b[i];}if(c[i]==2){s=" * ";ans=a[i]*b[i];}if(c[i]==3){if(b[i]!=0){if(ys==1){s=" ÷ ";ans=a[i]/b[i];}if(ys==2){if((a[i]%b[i])==0){s=" ÷ ";ans=a[i]/b[i];}else{flag=1;}}}elseflag=1;}ss[i]=a[i]+s+b[i];for(int i1=0;i1<i;i1++) //查重{if((a[i1]==a[i])&&(b[i1]==b[i])&&(c[i1]==c[i]))flag1=1; if((a[i1]==b[i])&&(b[i1]==a[i])&&(c[i1]==0)&&(c[i]==0))flag1=1;if((a[i1]==b[i])&&(b[i1]==a[i])&&(c[i1]==2)&&(c[i]==2))flag1=1;}if((flag==0)&&(flag1==0)) //输出{String sql="INSERT INTO size VALUES ('"+ss[i]+"','"+ans+"')";stmt.executeUpdate(sql);ans2[i]=""+ans;out.print(ss[i]);%><br><form name="form2" method="post" action="zscheck2.jsp">请输入答案<input name=<%=daan[i] %> type="text" id="daan"><br/> <% }else{i--;}}session.setAttribute("v1", ss);session.setAttribute("v2", ans2);%><input type="submit" value="统计结果"> </form><%}if(Integer.parseInt(weishu)==2){String j=request.getParameter("shuliang");int sl=Integer.valueOf(j);int litt=Integer.valueOf(zuix);int bi=Integer.valueOf(zuid);String []ss=new String[sl];String s="";String daan[] =new String [sl];int a[]=new int [sl];int b[]=new int [sl];int flag=0;int flag1=0;String [] ans2=new String[sl];int ans=0;int c[]=new int [sl];int d[]=new int[sl];int e[]=new int[sl];int fs=Integer.valueOf(fss);int ys=Integer.valueOf(yss);int y=0;int y1=0;int y2=0;String j1=request.getParameter("kuohao");int y3=Integer.valueOf(j1);for(int i=0;i<sl;i++){daan[i]="name"+i;int c1 []=new int [20000];flag1=0;y=(int )(Math.random()*4+3);//生成数的个数y1=(int )(Math.random()*2);//有无括号y2=(int )(Math.random()*2);//前后a[i]=(int)(Math.random()*(bi-litt)+1+litt);b[i]=(int)(Math.random()*(bi-litt)+1+litt);c[i]=(int)(Math.floor((Math.random()*4)));if(fs==1){flag=0;}if(fs==2)// 没有负数{if(c[i]==1){if((a[i]-b[i])>=0){flag=0;}else{flag=1;}}}if(c[i]==0){s=" + ";ans=a[i]+b[i];}if(c[i]==1){s=" - ";ans=a[i]-b[i];}if(c[i]==2){s=" * ";ans=a[i]*b[i];}if(c[i]==3){if(b[i]!=0){if(ys==1){s=" ÷ ";ans=a[i]/b[i];}if(ys==2){if((a[i]%b[i])==0){s=" ÷ ";ans=a[i]/b[i];}elseflag=1;}}elseflag=1;}ss[i]=a[i]+s+b[i];if(y3==1)//有括号生成{for(int r=0;r<(y-2);r++){y1=(int )(Math.random()*2);//有无括号y2=(int )(Math.random()*2);//前后d[r]=(int)(Math.random()*(bi-litt)+1+litt);c1[i]=(int )(Math.random()*4);if(c1[i]==0){s=" + ";ans=ans+d[r];}if(c1[i]==1){s=" - ";ans=ans-d[r];}if(c1[i]==2){s=" * ";ans=ans*d[r];}if(c1[i]==3){if(b[i]!=0){if(ys==1){s=" ÷ ";ans=ans/d[r];}if(ys==2){if((a[i]%b[i])==0){s=" ÷ ";ans=ans/d[r];}elseflag=1;}}elseflag=1;}if(y1==1) //随机数生成没有括号情况{if(y2==0) //加在后面{ss[i]=ss[i]+s+d[r];}if(y2==1) //加在前面{ss[i]=d[r]+s+ss[i];}}if(y1==0) //随机数生成了括号{if(y2==0) //加在后面{ss[i]="("+ss[i]+")"+s+d[r];}if(y2==1) //括号加在前边{ss[i]=d[r]+s+"("+ss[i]+")";}}for(int i2=0;i2<i;i2++){if(ss[i2]==ss[i])flag1=1;elseflag1=0;}}}if(y3==2){for(int r=0;r<(y-2);r++){y1=(int)(Math.random()*2);//有无括号y2=(int)(Math.random()*2);//前后d[r]=(int)(Math.random()*(bi-litt)+1+litt);c1[i]=(int)(Math.random()*4);if(c1[i]==0){s=" + ";ans=ans+d[r];}if(c1[i]==1){s=" - ";ans=ans-d[r];}if(c1[i]==2){s=" * ";ans=ans*d[r];}if(c1[i]==3){if(b[i]!=0){if(ys==1){s=" ÷ ";ans=ans/d[r];}if(ys==2){if((a[i]%b[i])==0){s=" ÷ ";ans=ans/d[r];}elseflag=1;}}elseflag=1;}if(y1==1) //没有括号{if(y2==0) //加在后面{ss[i]=ss[i]+s+d[r];}if(y2==1) {ss[i]=d[r]+s+ss[i];}}for(int i2=0;i2<i;i2++){if(ss[i2]==ss[i])flag1=1;elseflag1=0;}} //小for 循环}if((flag==0)&&(flag1==0)){String sql="INSERT INTO size VALUES ('"+ss[i]+"','"+ans+"')";stmt.executeUpdate(sql);ans2[i]=""+ans;out.print(ss[i]+" "); %><br><form name="form2" method="post" action="zscheck2.jsp">请输入答案<input name=<%=daan[i] %> type="text" id="daan"><br/> <%}elsei--;}session.setAttribute("v1", ss);session.setAttribute("v2", ans2);%><input type="submit" value="统计结果"> </form><%}%></body> </html>
结果核查
<%@ 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>Insert title here</title> </head> <body><%String[] s1= (String[])session.getAttribute("v1");String[] answer= (String[])session.getAttribute("v2");String[] daan=new String[s1.length];int count=0;//正确答案的个数for(int i=0;i<s1.length;i++){out.print("运算式为:");out.print(s1[i]+" ");out.print("您的输入结果为:");daan[i]=request.getParameter("name"+i);out.print(daan[i]+" ");out.print("正确结果为:");out.print(answer[i]);if(Integer.parseInt(daan[i])==Integer.parseInt(answer[i])){out.print("恭喜你答对了!");count++;}else{out.print("很遗憾,你答错了!");}out.print("<br>");}out.print("你一共答对的题目数:");out.print(count);%> </body> </html>
<%@ 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 ><form name="form3" method=post action="fes.jsp"><center> 整数四则运算</center><table align="center"><tr><td>请输入需要生成的题目数量</td><td><input name="shuliang" id="shuliang" type="text" > </td></tr><tr><td>请输入分数的最大值</td><td> <input name="zuida" id="zuida" type="text"> </td></tr><tr><td> 请选择是否需要括号1有2没有</td><td> <input name="kuohao" id="kuohao" type="text"> </td></tr><tr><td>请选择生成算式的位数1两位数2多位数</td><td><input name="weishu" id="weishu" type="text"> </td></tr><tr><td>加减有无负数</td><td><input name="fs" id="fs" type="text"> </td></tr><tr><td> 除法有无余数</td><td><input name="ys" id="ys"type="text"></td></tr></table><center><input type="submit" value="开始出题"> </center></form> </body> </html>
分数出题:
<%@ 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>Insert title here</title> </head> <body> <%!class gg{int gongbei(int x,int y){int z;for(z=x;;z++){if(z%x==0&&z%y==0)break;}return z;}int gongyue(int x,int y){int z;for(z=x;z>0;z--){if(x%z==0&&y%z==0)break;}return z;}} %> <%String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";Class.forName(driverStr).newInstance();String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=Webys";String dbusername = "sa";String dbpassword = "123456";Connection conn=null;java.sql.Statement stmt=null;conn=DriverManager.getConnection(connStr, dbusername, dbpassword);stmt=conn.createStatement();String weishu=request.getParameter("weishu");String kuohao=request.getParameter("kuohao");String zuid=request.getParameter("zuida");String fss=request.getParameter("fs");String yss=request.getParameter("ys");gg g=new gg();if(Integer.parseInt(weishu)==1){int fenzi=0;int fenmu=0;int huajian=0;String j=request.getParameter("shuliang");int sl=Integer.valueOf(j);int bi=Integer.valueOf(zuid);String []ss=new String[sl];String daan[] =new String [sl];String jj[]=new String[sl];String s="";int ans=0;String ans1[]=new String[sl];int a[]=new int [sl];int a1[]=new int[sl];int b[]=new int [sl];int b1[]=new int[sl];int flag=0;int flag1=0;int flag2=0;int flag3=0;int c[]=new int [sl];int d[]=new int[sl];int d1[]=new int[sl];int e[]=new int[sl];int fs=Integer.valueOf(fss);int ys=Integer.valueOf(yss);for(int i=0;i<sl;i++){daan[i]="name"+i;flag1=0;flag2=0;a[i]=(int)(Math.random()*(bi-1-1)+1);a1[i]=(int)(Math.random()*(bi-1)+1);b[i]=(int)(Math.random()*(bi-1-1)+1);b1[i]=(int)(Math.random()*(bi-1)+1);c[i]=(int)(Math.random()*4);if((a[i]>=a1[i])||(b[i]>=b1[i])) //判断是否为真分数{flag2=1;}else //是真分数继续进行{flag2=0;}if(fs==1) //有负数{flag=0;}if(fs==2) //没有负数{if(c[i]==1){if((a[i]*b1[i]-a1[i]*b[i])>=0){flag=0;}else{flag=1;}}}if(c[i]==0){s=" + ";fenzi=(a[i]*b1[i])+(a1[i]*b[i]);fenmu=a1[i]*b1[i];huajian=g.gongyue(fenzi, fenmu);int o=fenzi/huajian;int p=fenmu/huajian;if(o<p){jj[i]=o+"/"+p;}if((o>p)&&(o%p!=0)){int you=o/p;o=o-p*you;jj[i]=you+"|"+o+"/"+p;}if((o>p)&&(o%p==0)){int you=o/p;jj[i]=""+you;}}if(c[i]==1){s=" - ";fenzi=(a[i]*b1[i])-(a1[i]*b[i]);fenmu=a1[i]*b1[i];if(fenzi>0){huajian=g.gongyue(fenzi, fenmu);int o=fenzi/huajian;int p=fenmu/huajian;if(o<p){jj[i]=o+"/"+p;}if((o>p)&&(o%p!=0)){int you=o%p;o=o-p*you;jj[i]=you+"|"+o+"/"+p;}if((o>p)&&(o%p==0)){int you=o%p;jj[i]=""+you;}}if(fenzi<0){int t=0-fenzi;huajian=g.gongyue(t, fenmu);int o=fenzi/huajian;int p=fenmu/huajian;if(o<p){jj[i]=o+"/"+p;}if((o>p)&&(o%p!=0)){int you=o/p;o=o-p*you;jj[i]=you+"|"+o+"/"+p;}if((o>p)&&(o%p==0)){int you=o/p;jj[i]=""+you;}}if(fenzi==0){jj[i]=""+0;}}if(c[i]==2){s=" * ";fenzi=a[i]*b[i];fenmu=a1[i]*b1[i];huajian=g.gongyue(fenzi, fenmu);int o=fenzi/huajian;int p=fenmu/huajian;if(o<p){jj[i]=o+"/"+p;}if((o>p)&&(o%p!=0)){int you=o/p;o=o-p*you;jj[i]=you+"|"+o+"/"+p;}if((o>p)&&(o%p==0)){int you=o/p;jj[i]=""+you;}}if(c[i]==3){if(ys==1){s=" ÷ ";fenzi=a[i]*b1[i];fenmu=a1[i]*b[i];huajian=g.gongyue(fenzi,fenmu);int o=fenzi/huajian;int p=fenmu/huajian;if(o<p){jj[i]=o+"/"+p;}if((o>p)&&(o%p!=0)){int you=o/p;o=o-p*you;jj[i]=you+"|"+o+"/"+p;}if((o>p)&&(o%p==0)){int you=o/p;jj[i]=""+you;}}if(ys==2){if(((a[i]*b1[i])%(a1[i]*b[i]))==0){s=" ÷ ";fenzi=a[i]*b1[i];fenmu=a1[i]*b[i];huajian=g.gongyue(fenzi,fenmu);int o=fenzi/huajian;int p=fenmu/huajian;if(o<p){jj[i]=o+"/"+p;}if((o>p)&&(o%p!=0)){int you=o%p;o=o-p*you;jj[i]=you+"|"+o+"/"+p;}if((o>p)&&(o%p==0)){int you=o%p;jj[i]=you+"";}}else{flag=1;}}}int y=g.gongyue(a[i],a1[i]);int y1=g.gongyue(b[i],b1[i]);a[i]=a[i]/y;a1[i]=a1[i]/y;b[i]=b[i]/y1;b1[i]=b1[i]/y1;ss[i]=a[i]+"/"+a1[i]+s+b[i]+"/"+b1[i];for(int i1=0;i1<i;i1++) //查重{if((a[i1]==a[i])&&(a1[i1]==a1[i])&&(b[i1]==b[i])&&(b1[i]==b1[i1])&&(c[i1]==c[i]))flag1=1; if((a[i1]==b[i])&&(b[i1]==a[i])&&(c[i1]==0)&&(c[i]==0)&&(a1[i1]==b1[i])&&(b1[i1]==a1[i]))flag1=1;if((a[i1]==b[i])&&(b[i1]==a[i])&&(c[i1]==2)&&(c[i]==2)&&(a1[i1]==b1[i])&&(b1[i1]==a1[i]))flag1=1;}if((flag==0)&&(flag1==0)&&(flag2==0)) //输出{String sql="INSERT INTO size VALUES ('"+ss[i]+"','"+jj[i]+"')"; stmt.executeUpdate(sql);ans1[i]=jj[i];out.print(ss[i]);%><br><form name="form4" method="post" action="fscheck.jsp">请输入答案<input name=<%=daan[i] %> type="text" id="daan"><br/> <% }else{i--;}}session.setAttribute("v1", ss);session.setAttribute("v2", ans1);%><input type="submit" value="统计结果"> </form><%}if(Integer.parseInt(weishu)==2){String j=request.getParameter("shuliang");int sl=Integer.valueOf(j);int bi=Integer.valueOf(zuid);String []ss=new String[sl];String daan[] =new String [sl];String s="";int ans=0;String ans1[]=new String[sl];int a[]=new int [sl];int a1[]=new int[sl];int b[]=new int [sl];int b1[]=new int[sl];int flag=0;int flag1=0;int flag2=0;int flag3=0;int c[]=new int [sl];int d[]=new int[sl];int d1[]=new int[sl];int e[]=new int[sl];int fs=Integer.valueOf(fss);int ys=Integer.valueOf(yss);int y=0;int y1=0;int y2=0;for(int i=0;i<sl;i++){String j1=request.getParameter("kuohao");int y3=Integer.valueOf(j1);int c1 []=new int [20000];flag1=0;flag2=0;y=(int)(Math.random()*4+3);//生成数的个数y1=(int)(Math.random()*2);//有无括号y2=(int)(Math.random()*2);//前后a[i]=(int)(Math.random()*(bi-1-1)+1);a1[i]=(int)(Math.random()*(bi-1)+1);b[i]=(int)(Math.random()*(bi-1-1)+1);b1[i]=(int)(Math.random()*(bi-1)+1);c[i]=(int)(Math.random()*4);if((a[i]>=a1[i])||(b[i]>=b1[i])) //判断是否为真分数{flag2=1;}else //是真分数继续进行{flag2=0;}if(fs==1){flag=0;}if(fs==2)// 没有负数{if(c[i]==1){if((a[i]*b1[i]-a1[i]*b[i])>=0){flag=0;}else{flag=1;}}}if(c[i]==0){s=" + ";}if(c[i]==1){s=" - ";}if(c[i]==2){s=" * ";}if(c[i]==3){if(ys==1){s=" ÷ ";}if(ys==2){if(((a[i]*b1[i])%(a1[i]*b[i]))==0){s=" ÷ ";}else{flag=1;}}}int z=g.gongyue(a[i],a1[i]);int z1=g.gongyue(b[i],b1[i]);a[i]=a[i]/z;a1[i]=a1[i]/z;b[i]=b[i]/z1;b1[i]=b1[i]/z1;c1[i]=(int)(Math.random()*4);ss[i]=a[i]+"/"+a1[i]+s+b[i]+"/"+b1[i];if(y3==1)//有括号生成{for(int r=0;r<(y-2);r++){y1=(int)(Math.random()*2);//有无括号y2=(int)(Math.random()*2);//前后d[r]=(int)(Math.random()*(bi-1-1)+1);d1[r]=(int)(Math.random()*(bi-1)+1);int z3=g.gongyue(d[r],d1[r]);d[r]=d[r]/z3;d1[r]=d1[r]/z3;if(d[r]>=d1[r]){flag3=1;}else{flag3=0;}if(c1[i]==0){s=" + ";}if(c1[i]==1){s=" - ";}if(c1[i]==2){s=" * ";}if(c1[i]==3){if(ys==1){s=" ÷ ";}if(ys==2){if(y2==0){if((a[i]*b1[i])%(a1[i]*b[i])==0){s=" ÷ ";}elseflag=1;}}}if(y1==0) //随机数生成没有括号情况{if((y2==0)&&(flag3==0)) //加在后面{ss[i]=ss[i]+s+d[r]+"/"+d1[r];}if((y2==1)&&(flag3==0)) //加在前面{ss[i]=d[r]+"/"+d1[r]+s+ss[i];}}if(y1==1) //随机数生成了括号{if((y2==0)&&(flag3==0)) //加在后面{ss[i]="("+ss[i]+")"+s+d[r]+"/"+d1[r];}if((y2==1)&&(flag3==0)) //括号加在前边{ss[i]=d[r]+"/"+d1[r]+s+"("+ss[i]+")";}}for(int i2=0;i2<i;i2++){if(ss[i2]==ss[i])flag1=1;elseflag1=0;}if(flag3!=0)r--;}}if(y3==2) //没有括号{for(int r=0;r<(y-2);r++){c1[i]=(int)(Math.random()*4);y1=(int)(Math.random()*2);//有无括号y2=(int)(Math.random()*2);//前后d[r]=(int)(Math.random()*(bi-1-1)+1);d1[r]=(int)(Math.random()*(bi-1)+1);int z4=g.gongyue(d[r],d1[r]);d[r]=d[r]/z4;d1[r]=d1[r]/z4;if(d[r]>=d1[r]){flag3=1;}else{flag3=0;}if(c1[i]==0){s=" + ";}if(c1[i]==1){s=" - ";}if(c1[i]==2){s=" * ";}if(c1[i]==3){if(ys==1){s=" ÷ ";}if(ys==2){if((a[i]*b1[i])%(a1[i]*b[i])==0){s=" ÷ ";}elseflag=1;}}if(y1==1) //没有括号{if((y2==0)&&(flag3==0)) //加在后面{ss[i]=ss[i]+s+d[r]+"/"+d1[r];}if((y2==1)&&(flag3==0)){ss[i]=d[r]+"/"+d1[r]+s+ss[i];}}for(int i2=0;i2<i;i2++){if(ss[i2]==ss[i])flag1=1;elseflag1=0;}if(flag3!=0)r--;} //小for 循环}if((flag==0)&&(flag1==0)&&(flag2==0)&&(flag3==0)){out.print(ss[i]);%><br><form name="form5" method="post" action="fscheck.jsp">请输入答案<input name=<%=daan[i] %> type="text" id="daan"><br/> <% }elsei--;}session.setAttribute("v1", ss);session.setAttribute("v2", ans1);%><input type="submit" value="统计结果"> </form><%} %> </body> </html>
分数核查:
<%@ 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>Insert title here</title> </head> <body> <%String[] s1= (String[])session.getAttribute("v1");String[] answer= (String[])session.getAttribute("v2");String[] daan=new String[s1.length];int count=0;//正确答案的个数for(int i=0;i<s1.length;i++){out.print("运算式为:");out.print(s1[i]+" ");out.print("您的输入结果为:");daan[i]=request.getParameter("name"+i);out.print(daan[i]+" ");out.print("正确结果为:");out.print(answer[i]);if(daan[i].equals(answer[i])){out.print("恭喜你答对了!");count++;}else{out.print("很遗憾,你答错了!");}out.print("<br>");}out.print("你一共答对的题目数:");out.print(count);%> </body> </html>
三.运行结果截图:
4遇到的问题
在编程当中,遇到了很多麻烦,下面来阐述一下。首先,遇到的是怎么将菜单填入的值让自己和java里面用上,后来通过request方法来解决这个问题。还有就是随机数的生成,之前一直用的是Random类来实现随机数的生成,但是在JSP中好像不能直接那么用了,最后用的(int)(Math.random*n)方法。通过定义一个字符串数字,解决了文本框中输入值,和正确结果比对的问题。在有数据库连接方面,如果想在JSP中进行数据库的连接,在java中的驱语句后面还要加上.Instance()才能加载上。而且的Jdbc还要放在lib文件夹下。另外还有一个问题就是,在分数的情况下,执行SQL语句时,例如用1'1/5表示一又五分之时,会有二义性,最后将'改为|.