结对开发团队成员:赵子鸣、周宝辉
设计思想:
程序中涉及了简单的mvc框架中的jsp+javabean结构,将整数和分数式子的类写到src中entity的包中,将一些关于数据库的操作写到sql包中;jsp有三个,第一个是选择界面(选择一些简单的参数,例如整数或者分数,出题数据范围,算式运算符个数,式子个数等等),将这些参数传到第二个jsp中,在第二个jsp得到用户输入的参数,并且生成相应的算式题目,在第三个jsp中判断对错,在jsp中调用包中的类,直接使用.java中的分数整数类,在一些数据库操作中也是直接调用封装好的sql包中函数,来实现连接数据库,插入数据等操作。
其中,对于算式的计算是用递归实现的,比如3个数计算,可以先算一步,变成2个数的算式,由此可以递归来实现算式的结果。
源程序代码:
entity包
Fenshu.java
package entity; public class Fenshu { int a; int b; public static Fenshu jisuanjieguo(Fenshu a[],int b[],int n){ Fenshu jie=new Fenshu(); if(n==1){jie=jisuan(a[0],b[0],a[1]);} else { int ii=0;boolean z=false; for(int i=0;i<n;i++){ if(b[i]==3||b[i]==4){ ii=i; z=true; break; } } if(z==true){ a[ii]=jisuan(a[ii],b[ii],a[ii+1]); if(ii!=n-1){ for(int i=ii;i<n-1;i++){ b[i]=b[i+1]; a[i+1]=a[i+2]; } } jie=jisuanjieguo(a,b,n-1); } else { a[0]=jisuan(a[0],b[0],a[1]); for(int i=0;i<n-1;i++){ b[i]=b[i+1]; a[i+1]=a[i+2]; } jie=jisuanjieguo(a,b,n-1); } } return jie; } public static Fenshu jisuan(Fenshu aaa,int bbb,Fenshu ccc){ Fenshu d=new Fenshu(); int a1=1,b1=1; if(bbb==1){ b1=aaa.b*ccc.b; a1=aaa.a*ccc.b+aaa.b*ccc.a; } else if(bbb==2){ b1=aaa.b*ccc.b; a1=aaa.a*ccc.b-aaa.b*ccc.a; } else if(bbb==3){ b1=aaa.b*ccc.b; a1=aaa.a*ccc.a; } else { b1=aaa.b*ccc.a; a1=aaa.a*ccc.b; } d.set(a1,b1); d.yue(); return d; } public void set(int aa,int bb){ a=aa; b=bb; } public int zuidayin(){ int c=0,d=1; if(a<b) c=a; else c=b; for(int i=1;i<=c;i++){ if(a%i==0&&b%i==0){ d=i; } } return d; } public void yue(){ int y=this.zuidayin(); if(y!=1){ a=a/y; b=b/y; } } public String dai(){ String p=""; if(a>=b){ int q=a/b; int w=a-b*q; return p+q+"'"+w+"/"+b; } else { return p+a+"/"+b; } } public String fanhui(){ String r=""; r="("+a+"/"+b+")"; return r; } }
Fenshude.java
package entity; public class Fenshude { Fenshu as[]; int fu[]; int fanwei; int shu; public void getshu(int n){ shu=n; as=new Fenshu [n+1]; fu=new int [n]; } public void getfanwei(int a){ fanwei=a; } public void shengcheng(int pp){ for(int i=0;i<shu;i++){ if(pp==1) fu[i]=(int)(Math.random()*4+1); else fu[i]=(int)(Math.random()*2+1); } for(int i=0;i<shu+1;i++){ int ii=0; as[i]=new Fenshu(); while(ii==0){ as[i].b=(int)(Math.random()*(fanwei-1)+1); as[i].a=(int)(Math.random()*(fanwei-1)+1); if(as[i].a<as[i].b){ii=1;} } } } public String zhuan(int a){ if(a==1) return "+"; else if(a==2) return "-"; else if(a==3)return "*"; else return "/"; } public String show(){ String uuu=""; for(int i=0;i<shu;i++){ uuu="("+as[i].a+"/"+as[i].b+") "+zhuan(fu[i]); System.out.print("("+as[i].a+"/"+as[i].b+") "+zhuan(fu[i])+" "); } uuu=uuu+"("+as[shu].a+"/"+as[shu].b+")"; System.out.print("("+as[shu].a+"/"+as[shu].b+")"); System.out.println(" ="); return uuu; } public Fenshu jieguo(){ Fenshu jieguo=new Fenshu(); jieguo=Fenshu.jisuanjieguo(as,fu,shu); return jieguo; } public Fenshu showjieguo(){ Fenshu ee=jieguo(); System.out.println(" 结果为:"+ee.a+"/"+ee.b); return ee; } public String showshizi(){ String r=""; for(int i=0;i<shu;i++){ r=r+as[i].fanhui()+zhuan(fu[i]); } r=r+as[shu].fanhui(); return r; } public String showdaan(){ String r=""; Fenshu ee=jieguo(); r=r+ee.a+"/"+ee.b; if(ee.b==1) r=""+ee.a; return r; } }
Panduan.java
package entity; public class Panduan{ public static int panduan1(String b) { if(b.equals("2")) return 2; else if(b.equals("3")) return 3; else if(b.equals("4")) return 4; else if(b.equals("5")) return 5; else return 0; } public static int panduan2(String b) { if(b.equals("10")) return 10; else if(b.equals("50")) return 50; else if(b.equals("100")) return 100; else return 0; } public static void main(String[]arg) { String a="2"; int m=panduan1(a); System.out.println(m); int n=panduan2("10"); System.out.println(n); } }
Suan.java
package entity; public class Suan { int fanwei; int shu; int zheng[]; int fu[]; public void getshu(int n){ shu=n; zheng=new int [n+1]; fu=new int [n]; } public void getfanwei(int a){ fanwei=a; } public void shengcheng(int pp){ for(int i=0;i<shu;i++){ if(pp==1) fu[i]=(int)(Math.random()*4+1); else fu[i]=(int)(Math.random()*2+1); } for(int i=0;i<shu+1;i++){ zheng[i]=(int)(Math.random()*(fanwei-1)+1); } for(int i=0;i<shu;i++){ int aa=1; while(aa==1){ if(fu[i]==2){ if(zheng[i]>=zheng[i+1]){ aa=0; } else{ zheng[i]=(int)(Math.random()*(fanwei-1)+1); zheng[i+1]=(int)(Math.random()*(fanwei-1)+1); } } else if(fu[i]==4){ if(zheng[i]<zheng[i+1]){ aa=0; } else{ zheng[i]=(int)(Math.random()*(fanwei-1)+1); zheng[i+1]=(int)(Math.random()*(fanwei-1)+1); } } else{ aa=0; } } } } public String zhuan(int a){ if(a==1) return "+"; else if(a==2) return "-"; else if(a==3)return "*"; else return "/"; } public String show(){ String uuu=""; for(int i=0;i<shu;i++){ uuu=uuu+zheng[i]+zhuan(fu[i]); System.out.print(zheng[i]+" "+zhuan(fu[i])+" "); } uuu=uuu+zheng[shu]; System.out.print(zheng[shu]); System.out.println(" ="); return uuu; } public Fenshu jieguo(){ Fenshu qq[]=new Fenshu[shu+1]; for(int i=0;i<shu+1;i++){ qq[i]=new Fenshu(); qq[i].set(zheng[i],1); } Fenshu jieguo=new Fenshu(); jieguo=Fenshu.jisuanjieguo(qq,fu,shu); return jieguo; } public Fenshu showjieguo(){ Fenshu ee=jieguo(); System.out.println(" 结果为:"+ee.a+"/"+ee.b); return ee; } public String showdaan(){ String r=""; Fenshu ee=jieguo(); r=r+ee.a+"/"+ee.b; if(ee.b==1) r=""+ee.a; return r; } public String showshizi(){ String r=""; for(int i=0;i<shu;i++){ r=r+zheng[i]+zhuan(fu[i]); } r=r+zheng[shu]; return r; } public String fanhui(){ String aq=""; for(int i=0;i<shu;i++){ aq=aq+""+zheng[i]+zhuan(fu[i]); } aq=aq+zheng[shu]; return aq; } }
sql包
Sql.java
package sql; import java.sql.*; public class Sql { static Connection con=null; static Statement stmt=null; static PreparedStatement psmt=null; public static Statement get(){ return stmt; } public static void lianjie(){ try{ Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/web_number","root","baohui"); stmt=con.createStatement(); System.out.print("数据库连接成功"); //out.print("<p>"); } catch(Exception e) { System.out.print(e); } } public static void insert (int a,String b,String c)throws Exception{ try { String sql2="insert into web (number,shizi,jieguo) values (?,?,?)"; psmt=con.prepareStatement(sql2); psmt.setInt(1,a); psmt.setString(2,b); psmt.setString(3,c); psmt.executeUpdate(); //out.print("插入成功!"); } catch(Exception e) { //out.print(e); } } public static void delete()throws Exception{ String sql1="delete from web"; stmt.execute(sql1); } public static void guan()throws Exception{ psmt.close(); stmt.close(); con.close(); } public static void main(String arg[]){ lianjie(); } }
jsp
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@page import="entity.*"%> <%@page import="sql.*"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>出题</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form id=form1 method="post" action="judge.jsp" > <% request.setCharacterEncoding("utf-8"); String operator=request.getParameter("operator"); String operator1=request.getParameter("operator1"); String number1=request.getParameter("number1"); String number2=request.getParameter("number2"); int a=Panduan.panduan1(number1); int b=Panduan.panduan2(number2); int c=2; if(operator.equals("是")) { c=1; } if(operator1.equals("整数")) { Suan xx[]=new Suan[10]; int wei=b; int shu=a; String pp=""; String pp1=""; Sql.lianjie(); Sql.delete(); for(int i=0;i<10;i++){ xx[i]=new Suan(); xx[i].getfanwei(wei); xx[i].getshu(shu); xx[i].shengcheng(c); pp=xx[i].show(); pp1=xx[i].showjieguo().dai(); out.print(xx[i].showshizi()+"= "); //out.print(xx[i].showdaan()); out.print("<input type=text id=text1 name=name"+i+">"); out.print("<p>"); Sql.insert(i+1,xx[i].showshizi(),xx[i].showdaan()); } Sql.guan(); } else { request.setCharacterEncoding("utf-8"); Fenshude xx[]=new Fenshude[10]; int wei=b; int shu=a; String pp=""; String pp1=""; Sql.lianjie(); Sql.delete(); for(int i=0;i<10;i++){ xx[i]=new Fenshude(); xx[i].getfanwei(wei); xx[i].getshu(shu); xx[i].shengcheng(c); //pp=xx[i].show(); //pp1=xx[i].showjieguo().dai(); out.print(xx[i].showshizi()+"= "); //out.print(xx[i].showdaan()); out.print("<input type=text id=text1 name=name"+i+">"); out.print("<p>"); Sql.insert(i+1,xx[i].showshizi(),xx[i].showdaan()); } Sql.guan(); } //out.print(a+"<p>"); //out.print(b+"<p>"); %> <input type=submit name=submit value=提交 > </form> </body> </html>
judge.jsp
<%@ page language="java" import="java.util.*" import ="java.sql.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@page import="entity.*"%> <%@page import="sql.*"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>判断答案</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% request.setCharacterEncoding("utf-8"); Statement stmt=null; Connection con=null; try{ Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection("jdbc:mysql://127.0.0.1/web_number","root","baohui"); stmt=con.createStatement(); //out.print("数据库连接成功"); //out.print("<p>"); } catch(Exception e) { out.print(e); } //Sql.lianjie(); String sql="select * from web"; ResultSet rs=stmt.executeQuery(sql); for(int i=0;i<10;i++) { //sql语句执行 int number=0; String shizi=null; String jieguo=null; if(rs.next()) { number=rs.getInt("number"); shizi=rs.getString("shizi"); jieguo=rs.getString("jieguo"); } //进行比较,判断对错 out.print(shizi+"= "); String name=request.getParameter("name"+i); out.print(name); String name1=jieguo; if(name.equals(name1)) { out.print(" "); out.print("<font color=greeen face=楷体>正确</font>"); } else { out.print(" "); out.print("<font color=red face=楷体>错误</font>"); } out.print("<p>"); } stmt.close(); con.close(); %> </body> </html>
select.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>选择参数</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form id=form2 method=post action="index.jsp"> <table align=center border=0> <tr> <td>是否需要乘除:<td> <td>是 <input type=radio name=operator value="是" checked> 否 <input type=radio name=operator value="否"></td> </tr> <tr> <td>选择整数还是分数:<td> <td>整数<input type=radio name=operator1 value="整数" checked> 分数<input type=radio name=operator1 value="分数"></td> </tr> <tr> <td>运算符的个数:<td> <td><select name=number1 size=1> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> </td> </tr> <tr> <td>请选择运算范围:<td> <td> <select name=number2 size=1> <option>10</option> <option>50</option> <option>100</option> </select> </td> </tr> <tr> <td><input type=submit name=submit value=下一步 ></td> </tr> </table> </form> </body> </html>
运行结果截图:
总结体会:
首先,由于题目要求比较多,所以在编程之前的设计思路是非常重要的,将需要封装的封装为类,相应的一些功能函数应当写成函数以便后面可以灵活的调用;计算结果的过程利用的是递归的方法,此处是把握了每次计算的规律,找到相似的地方,在递归函数的实现上,起初总是没结果,后来发现没有传递回来值,所以递归中返回值也是很重要的;在数据库的连接时,需要注意jdbc的.jar文件的配置,否则数据库会驱动失败(比较容易遗漏);在jsp中使用循环结构生成式子的时候,因为要留出答题的文本框,所以在循环需要注意每输出一个式子,相应的输出一个文本框,这里主要答题方便,使界面更具有美观性,操作性;在jsp传递参数过程中涉及到类型转换,需要注意;用javabean来封装类使得思路比较清晰,结构框架清楚,把java代码实现的语句大部分写到.java文件中,而在.jsp文件中主要写传参以及界面的设计,使得思路更具有逻辑性。
psp表