结对编程——四则运算
Posted YOUNG111
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了结对编程——四则运算相关的知识,希望对你有一定的参考价值。
1.实验要求:
2.实验思路:使用jsp Javabean和servlet来实现,Javabean定义实体类,定义能根据设置的参数产生出对应的方法,jsp页面用来让用户选择参数,做题和查看历史记录,servlet根据jsp传的数据进行响应和处理。
首先有一个选择界面的jsp,如果选择做题就跳转到设置参数的jsp,设置好参数后,传到servlet,servlet根据传过来的参数产生题目,存储好以后,跳转到显示题目的jsp页面,用户可以输入答案,当交卷的时候会提交到判断的servlet,servlet根据传过的答案和正确答案进行比较,并输出做对和做错的题号,然后输出每道题和每道题的判断结果;如果选择的是查看历史记录,就会查看以前所做的题目。
3.实验代码:
//选择做题还是查询历史记录
<%@ 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> <h1>请选择是做题还是查询历史记录</h1> <hr> <br> <br> <br> <a href="setParam.jsp"><input type="button" value="开始做题"></a> <a href="selectServlet"><input type="button" value="查询历史记录"></a> </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"> <style> span { color:blue; } </style> <script type="text/javascript"> $(function() { $(\'#formbackground\').height($(window).height()); $(\'#formbackground\').width($(window).width()); } ); </script> <script type="text/javascript"> function $(id) { return document.getElementById(id); } function check() { var num=$("num").value; var scope=$("scope").value; $("numinfo").innerHTML=""; $("scopeinfo").innerHTML=""; if(num=="") { $("numinfo").innerHTML="题数不能为空,请输入题数"; $("num").focus(); return false; } if(scope=="") { $("scopeinfo").innerHTML="取值范围不能为空,请输入范围"; $("scope").focus(); return false; } return true; } </script> <title>Insert title here</title> </head> <body> <h1>请选择做题的类型</h1> <br> <form action="setParamServlet" method="post" onsubmit="return check()"> <table> <tr> <td>类型:</td> <td>整数<input type="radio" name="type" value="2" checked="checked"></td> <td>真分数<input type="radio" name="type" value="1"></td> </tr> <tr> <td>题数:</td> <td><input type="text" name="num" id="num"><span id="numinfo"></span></td> </tr> <tr> <td>取值范围:</td> <td><input type="text" name="scope" id="scope"><span id="scopeinfo"></span></td> </tr> <tr> <td>是否含有括号</td> <td>无<input type="radio" name="isBracket" value="2" checked="checked"></td> <td>有<input type="radio" name="isBracket" value="1"></td> </tr> <tr> <td><input type="submit" value="确认"> <input type="reset" value="取消"></td> </tr> </table> </form> </body> </html> 设置参数jsp
//产生题目的servlet
package servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import util.publicUse; /** * Servlet implementation class setParamServlet */ @WebServlet("/setParamServlet") public class setParamServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public setParamServlet() { super(); // TODO Auto-generated constructor stub } /** * @see Servlet#init(ServletConfig) */ public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub } /** * @see Servlet#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // TODO Auto-generated method stub publicUse P = new publicUse(); response.setContentType("text/html;charset=utf-8"); String type;//题目类型 int num;//题数 int scope;//范围 String isBracket;//是否含有括号 type=request.getParameter("type"); num=Integer.parseInt(request.getParameter("num")); scope=Integer.parseInt(request.getParameter("scope")); isBracket=request.getParameter("isBracket"); PrintWriter out = response.getWriter(); System.out.println(type+"\\t"+num+"\\t"+scope+"\\t"+isBracket); int choose1=Integer.parseInt(type); int choose2=Integer.parseInt(isBracket); try { String rs[]=new String[2*num]; rs=P.operationAndStatistical(choose1, choose2, num,scope); String []bds = new String[num];//存取表达式 String []rs1 = new String[num];//存取结果 for(int i=0;i<num;i++) { bds[i] = rs[i]; rs1[i] = rs[i + num]; } request.getSession().setAttribute("bds", bds); request.getSession().setAttribute("rs1", rs1); request.getSession().setAttribute("num", num); request.getRequestDispatcher("show.jsp").forward(request, response); // response.sendRedirect(request.getContextPath()+"/show.jsp"); } catch (ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 产生题目的servlet
//显示题目
<%@ 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> <script type="text/javascript"> function $(id) { return document.getElementById(id); } function check(){ var result = document.getElementsByName(\'result\'); for(var i = 0;i < result.length;i++) { if(result[i].value=="") { $("" + i).innerHTML = "请输入答案"; result[i].focus(); return false; } else{ $("" + i).innerHTML =""; result[i].focus(); } } function init(){ } return true; } </script> </head> <body> <h1>请在此处答题:</h1> <hr> <% %> <form action="judgeServlet" method="post" onsubmit="return check()"> <table> <% int num=(Integer)request.getSession().getAttribute("num"); String []a=(String[])request.getSession().getAttribute("bds"); for(int i=0;i<num;i++) { %><tr> <td>请作答第<%=i+1 %>道题:</td> <td><%=a[i] %></td> <td><input type="text" name="result" /><span id=<%=i %>></span></td> </tr> <% } %> <tr> <td><input type="submit" value="交卷"></td> </tr> </table> </form> </body> </html> 做题页面jsp
package servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import entity.data; import jdbc.insertsj; import util.solve; /** * Servlet implementation class judgeServlet */ @WebServlet("/judgeServlet") public class judgeServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public judgeServlet() { super(); // TODO Auto-generated constructor stub } /** * @see Servlet#init(ServletConfig) */ public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub } /** * @see Servlet#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); String []bds =(String[]) request.getSession().getAttribute("bds");//获取表达式 String []rs =(String[]) request.getSession().getAttribute("rs1");//获取正确结果 String []inputrs = request.getParameterValues("result");//获取输入的结果 solve s = new solve(); boolean[]sz = s.judgeIfTrue(rs, inputrs);//判断是否正确 int[] idnum = new int[rs.length];//存取题号 int[]count = s.zongjie(sz); String[]qiq = s.qiq(sz); String[]count1 = s.countexpression(bds, rs, inputrs, sz); out.println("正确的题数"+count[0]+"<br>"); out.println("错误的题数"+count[1]+"<br>"); out.println(qiq[0]+"<br>"); out.println(qiq[1]+"<br>"); for(int i=0;i<rs.length;i++) { idnum[i]=i+1; out.println(count1[i]+"<br>"); } data []a = new data[rs.length]; for(int i=0;i<rs.length;i++) { a[i] = new data(); a[i].setId(idnum[i]); a[i].setTitleexception(bds[i]); a[i].setResult(rs[i]); a[i].setInputrs(inputrs[i]); a[i].setIftrue(sz[i]); } insertsj out1 =new insertsj(); try { out1.cunchu(a); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // response.sendRedirect(request.getContextPath()+"/showResult.jsp"); // request.getRequestDispatcher("showResult.jsp").forward(request, response); out.print("<a href=\'choose.jsp\'>返回</a>"); } } 判断是否正确,并将结果输出的servlet和和讲题目存档
package servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import entity.data; import jdbc.insertsj; import util.solve; /** * Servlet implementation class judgeServlet */ @WebServlet("/judgeServlet") public class judgeServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public judgeServlet() { super(); // TODO Auto-generated constructor stub } /** * @see Servlet#init(ServletConfig) */ public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub } /** * @see Servlet#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); String []bds =(String[]) request.getSession().getAttribute("bds");//获取表达式 String []rs =(String[]) request.getSession().getAttribute("rs1");//获取正确结果 String []inputrs = request.getParameterValues("result");//获取输入的结果 solve s = new solve(); boolean[]sz = s.judgeIfTrue(rs, inputrs);//判断是否正确 int[] idnum = new int[rs.length];//存取题号 int[]count = s.zongjie(sz); String[]qiq = s.qiq(sz); String[]count1 = s.countexpression(bds, rs, inputrs, sz); out.println("正确的题数"+count[0]+"<br>"); out.println("错误的题数"+count[1]+"<br>"); out.println(qiq[0]+"<br>"); out.println(qiq[1]+"<br>"); for(int i=0;i<rs.length;i++) { idnum[i]=i+1; out.println(count1[i]+"<br>"); } data []a = new data[rs.length]; for(int i=0;i<rs.length;i++) { a[i] = new data(); a[i].setId(idnum[i]); a[i].setTitleexception(bds[i]); a[i].setResult(rs[i]); a[i].setInputrs(inputrs[i]); a[i].setIftrue(sz[i]); } insertsj out1 =new insertsj(); try { out1.cunchu(a); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // response.sendRedirect(request.getContextPath()+"/showResult.jsp"); // request.getRequestDispatcher("showResult.jsp").forward(request, response); out.print("<a href=\'choose.jsp\'>返回</a>"); } } 判断是否正确,并将结果输出的servlet和和讲题目存档
<%@ 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> <h1>历史记录页面</h1> <% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String s=(String)request.getSession().getAttribute("s1"); String []s1 = s.split(s); if(s1.length==0) { %>你还没有历史记录<% } else { for(int i=0;i<s1.length;i++) { out.print(s1[i] + "<br>"); } } %> <a href="choose.jsp">返回</a> </body> </html> 显示做题记录的jsp
我把需要用的方法和用调用的参数都已经分好类:
如下图:
entity用来存取实体类,也就是数据库对应的那些参数,一开始写了三个,最后只有一个用上了,内容如下:
package entity; public class data { private int id;//题号 private String titleexception;//表达式 private String result;//正确结果 private String inputrs;//输入结果 private boolean iftrue;//判断对错 public data() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitleexception() { return titleexception; } public void setTitleexception(String titleexception) { this.titleexception = titleexception; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String getInputrs() { return inputrs; } public void setInputrs(String inputrs) { this.inputrs = inputrs; } public boolean isIftrue() { return iftrue; } public void setIftrue(boolean iftrue) { this.iftrue = iftrue; } } entity
第二个是jdbc,也就是连接数据库,查询和插入的方法:
package jdbc; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; import entity.data; public class insertsj { public void cunchu(data[]a) throws SQLException { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = (Connection) JdbcUtils.getConnection(); String sql = "insert into wzw3 (idnum,titleexception,result,inputrs,iftrue) values(?,?,?,?,?)"; ps = conn.prepareStatement(sql); for(int k=0;k<a.length;k++) { ps.setInt(1, a[k].getId()); ps.setString(2, a[k].getTitleexception()); ps.setString(3, a[k].getResult()); ps.setString(4, a[k].getInputrs()); ps.setBoolean(5, a[k].isIftrue()); ps.executeUpdate(); } } finally { JdbcUtils.free(rs, ps, conn); } } } 插入的
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public final class JdbcUtils { private static String url = "jdbc:mysql://localhost:3306/wzw1"; private static String user = "wzw1"; private static String password = "121203"; private JdbcUtils() {} static { try{ Class.forName("com.mysql.jdbc.Driver"); }catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,user,password); } public static void free(ResultSet rs, Statement ps, com.mysql.jdbc.Connection conn) throws SQLException { // TODO Auto-generated method stub try{ if(rs!=null) { rs.close(); } } catch(SQLException e) { e.printStackTrace(); } finally { try{ if(ps!=null) ps.close(); } catch(SQLException e) { e.printStackTrace(); } finally { if(conn!=null) { conn.close(); } } } } public static void free(ResultSet rs, PreparedStatement ps, com.mysql.jdbc.Connection conn) throws SQLException { // TODO Auto-generated method stub try{ if(rs!=null) { rs.close(); } } catch(SQLException e) { e.printStackTrace(); } finally { try{ if(ps!=null) ps.close(); } catch(SQLException e) { e.printStackTrace(); } finally { if(conn!=null) { conn.close(); } } } } } jdbc连接的工具类
package jdbc; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.Connection; public class select { public static String selectbyIdTime() throws SQLException { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ conn = (Connection) JdbcUtils.getConnection(); String sql="select * from wzw3"; ps = conn.prepareStatement(sql); rs=ps.executeQuery(); String a1 = ""; while(rs.next()) { a1+=rs.getInt("idnum")+rs.getString("titleexception")+rs.getString("result")+rs.getString("inputrs")+rs.getBoolean("iftrue") + "#"; } return a1; } finally { JdbcUtils.free(rs, ps, conn); } } } 查询
第三个是用来相应的servlet,在上面都已经展示过了
第四个是产生题目的方法,跟上一次写的差不多,但是因为太多,我把分成三个Java文件,还有一个是判断所做的体是否正确的方法:
package util; public class integer { //整数运算 public static String generateExpressionkh(int num,int scope)//产生带括号的整数表达式 { publicUse u = new publicUse(); int a1[]=new int[num]; int a2[]=new int[num-1]; int a3[]=new int[num]; String[]a5=new String[num]; String[] a4={"+","-","*","/"}; for(int i=0;i<num;i++) { a1[i]=(int) (Math.random()*(scope-1)+1); } for(int i=0;i<num-1;i++) { a2[i]=(int) (Math.random()*4); } a3=u.chansheng(num); for(int i=0;i<num;i++) { a5[i]=""; if(a3[i]<0) { int c=0-a3[i]; for(int j=0;j<c;j++) { a5[i]+=")"; } } else { for(int j=0;j<a3[i];j++) { a5[i]+="("; } } } String t=""; for(int i=0;i<num-1;i++) { if(a3[i]>0) { t+=a5[i]+" "+a1[i]+" "+a4[a2[i]]; } else { t+=" "+a1[i]+" "+a5[i]+a4[a2[i]]; } } if(a3[num-1]>0) { t+=a5[num-1]+" "+a1[num-1]+" "; } else { t+=" "+a1[num-1]+" "+a5[num-1]; } return t; } public static String generationexception(int num,int scope)//产生不带括号的表达式 { int a1[]=new int[num]; int a2[]=new int[num-1]; int a3[]=new int[num]; String[] a4={"+","-","*","/"}; for(int i=0;i<num;i++) { a1[i]=(int) (Math.random()*(scope-1)+1); } for(int i=0;i<num-1;i++) { a2[i]=(int) (Math.random()*4); } String t=""; for(int i=0;i<num-1;i++) { if(a3[i]>0) { t+=" "+a1[i]+" "+a4[a2[i]]; } else { t+=" "+a1[i]+" "+a4[a2[i]]; } } if(a3[num-1]>0) { t+=" "+a1[num-1]+" "; } else { t+=" "+a1[num-1]+" "; } return t; } } 产生整数的题目
package util; public class properFraction { //真分数运算 public static String properFractionExit(int num,int scope)//产生不含括号含有真分数的表达式 { publicUse u =new publicUse(); int []r1=new int[2*num];//接受产生的数值 int []r2=new int[num-1];//接受符号 String[]r3={"+","-","*","/"}; String rs="";//接受含括号的和不含括号的表达式 char ch=\'z\'; while(ch==\'z\') { int i=0; for(;i<2*num;i++) { r1[i]=(int) (Math.random()*(scope-1)+1); } ch=\'y\'; int j=0; while(j<2*num) { if(r1[j]>=r1[j+1]) { ch=\'z\'; break; } j++; j++; } } for(int i=0;i<num-1;i++) { r2[i]=(int) (Math.random()*4); } int j=0; while(j<2*num-2) { int commondivisor=u.maxyue(r1[j], r1[j+1]); r1[j]/=commondivisor; r1[j+1]/=commondivisor; if(r1[j]==r1[j+1]) { rs+=" "+1+" "; } else { rs+=" "+r1[j]+"/"+r1[j+1]+" "+r3[r2[(j+1)/2]]; } j++;以上是关于结对编程——四则运算的主要内容,如果未能解决你的问题,请参考以下文章