首页 > 代码库 > 软件工程结对作业01

软件工程结对作业01

结对人:胡建峰、高雪彤

 

设计思想

在以前的程序基础上两人结合开发web系统。

首先编写了一个登录界面,实现的是不同用户,不同题库的功能。

然后进入主页,主页中在登录成功后显示用户名,并有开始答题,查看历史错题和注销三个按钮。开始答题按钮可以供用户选择出题的条件生成题目,查看历史错题会调出数据库中历史的错题,也可以重新做这些错题,注销会注销当前用户,返回登录界面。

出题界面会显示用户的条件,在下面生成题目,后面有一个输入框,供用户答题,不会的可以空下,最后点击提交可以进入结果验证页面,结果页面中,会对输入和信息与数据库中的作对比,正确的显示一个正确的图片,错误会显示一个错误的图片并显示正确答案,并将错误答案和信息存入数据库。

重做错题,从数据库中导出错题和错误答案,在修改提交后,会进行判断。

在最后对页面使用了Javabean、servlet、ajax、jQuery,并对页面进行了美化。

 

源程序代码

 

技术分享
 1 package bean;
 2 
 3 import java.sql.*;
 4 import com.db.DB;
 5 
 6 public class User {
 7     private String username;
 8     private String userpass;
 9     
10     public String getUsername() {
11         return username;
12     }
13 
14     public void setUsername(String username) {
15         this.username = username;
16     }
17 
18     public String getUserpass() {
19         return userpass;
20     }
21 
22     public void setUserpass(String userpass) {
23         this.userpass = userpass;
24     }
25 
26     @SuppressWarnings("static-access")
27     public boolean validate(String user, String pass) {
28         DB db = new DB();
29         String sq = "select * from [user] where [id]=? and [pass]=?";
30          PreparedStatement st = null;
31          try {
32             st = db.getCoon().prepareStatement(sq);
33             st.setString(1, user);
34              st.setString(2, pass);
35              ResultSet rs = st.executeQuery();
36              if(rs.next())
37             {
38                  return true;
39             }
40              else
41              {
42                  return false;
43              }
44         } catch (SQLException e) {
45             e.printStackTrace();
46         }
47         return false;     
48     }
49 
50 }
User.java

 

 

 

技术分享
  1 package calculat;
  2 import java.util.Random;
  3 
  4 public class Cal_zfs{
  5     private char fh;
  6     private String shu1;
  7     private String shu2;
  8     private String result;
  9     
 10     public char getFh() {
 11         return fh;
 12     }
 13 
 14     public String getResult() {
 15         return result;
 16     }
 17 
 18     public String getShu1() {
 19         return shu1;
 20     }
 21 
 22     public String getShu2() {
 23         return shu2;
 24     }
 25     public Cal_zfs()
 26     {
 27         
 28     }
 29     
 30     public Cal_zfs(boolean cc, int w)
 31     {
 32         int fzi1;
 33         int fmu1;
 34         int fzi2;
 35         int fmu2;
 36         char [] fu = new char [4];//生成符号,控制有无乘除
 37         fu[0] = ‘-‘;
 38         fu[1] = ‘+‘;
 39         fu[2] = ‘/‘;
 40         fu[3] = ‘*‘;
 41         if(cc)
 42             fh = fu[new Random().nextInt(4)];
 43         else
 44             fh = fu[new Random().nextInt(2)];
 45         
 46         
 47         if(w == 1)
 48         {    
 49             fmu1 = new Random().nextInt(9) + 1;
 50             fmu2 = new Random().nextInt(9) + 1;
 51         }
 52         else if(w == 2)
 53         {
 54             fmu1 = new Random().nextInt(99) + 1;
 55             fmu2 = new Random().nextInt(99) + 1;
 56         }
 57         else
 58         {
 59             fmu1 = new Random().nextInt(999) + 1;
 60             fmu2 = new Random().nextInt(999) + 1;
 61         }
 62         
 63         fzi1 = new Random().nextInt(fmu1) + 1;
 64         fzi2 = new Random().nextInt(fmu2) + 1;
 65         int gy = gys(fzi1, fmu1);
 66         int gy2 = gys(fzi2, fmu2);
 67         if(fzi1 == fmu1)
 68             shu1 = "1";
 69         else
 70             shu1 = fzi1 / gy + "/" + fmu1 / gy;
 71         if(fzi2 == fmu2)
 72             shu2 = "1";
 73         else
 74             shu2 = fzi2 / gy2 + "/" + fmu2 / gy2;
 75         
 76         int fm = 0, fz = 0;
 77         if(fh == ‘+‘)
 78         {
 79             fm = fmu1 * fmu2;
 80             fz = fmu1 * fzi2 + fmu2 * fzi1;
 81         }
 82         else if(fh == ‘-‘)
 83         {
 84             fm = fmu1 * fmu2;
 85             fz = fmu2 * fzi1 - fmu1 * fzi2;
 86         }
 87         else if(fh == ‘*‘)
 88         {
 89             fm = fmu1 * fmu2;
 90             fz = fzi1 * fzi2;
 91         }
 92         else
 93         {
 94             fm = fmu1 * fzi2;
 95             fz = fzi1 * fmu2;
 96         }
 97         
 98         int g = gys(fz, fm);
 99         if(fz == fm)
100             result = "1";
101         else
102             result = fz / g + "/" + fm / g;
103     }
104     
105     public int gys(int s1, int s2)
106     {
107         int gy = 1;
108         for(int i = 1; i <= (s1 < s2 ? s1 : s2); i++)
109         {
110             if(s1 % i == 0 && s2 % i == 0)
111                 gy = i;
112         }
113         return gy;
114     }
115     
116     public String show()
117     {
118         return shu1 + " " + fh + " " + shu2;
119     }
120 }
Cal_zfs.java

 

 

 

技术分享
  1 package calculat;
  2 import java.util.Random;
  3 
  4 public class Cal_zs{
  5     private char fh;
  6     private int shu1;
  7     private int shu2;
  8     private String result;
  9     
 10     public String getResult() {
 11         return result;
 12     }
 13     public Cal_zs()
 14     {}
 15     public char getFh() {
 16         return fh;
 17     }
 18 
 19     public int getShu1() {
 20         return shu1;
 21     }
 22 
 23     public int getShu2() {
 24         return shu2;
 25     }
 26     public Cal_zs(boolean cc,int wei)
 27     {
 28         char [] fu = new char [4];//生成符号,控制有无乘除
 29         fu[0] = ‘-‘;
 30         fu[1] = ‘+‘;
 31         fu[2] = ‘/‘;
 32         fu[3] = ‘*‘;
 33          
 34         if(cc)
 35             fh = fu[new Random().nextInt(4)];
 36         else
 37             fh = fu[new Random().nextInt(2)];
 38         if(wei == 1)
 39         {    
 40             shu1 = new Random().nextInt(9) + 1;
 41             shu2 = new Random().nextInt(9) + 1;
 42         }
 43         else if(wei == 2)
 44         {
 45             shu1 = new Random().nextInt(99) + 1;
 46             shu2 = new Random().nextInt(99) + 1;
 47         }
 48         else
 49         {
 50             shu1 = new Random().nextInt(999) + 1;
 51             shu2 = new Random().nextInt(999) + 1;
 52         }
 53         
 54         if(fh == ‘+‘)
 55             result = shu1 + shu2 + "";
 56         else if(fh == ‘-‘)
 57             result = shu1 - shu2 + "";
 58         else if(fh == ‘*‘)
 59             result = shu1 * shu2 + "";
 60         else
 61         {
 62             if(shu1 >= shu2)
 63             {
 64                 if(shu1 % shu2 == 0)
 65                     result = shu1/shu2 + "";
 66                 else
 67                 {
 68                     int z = shu1 / shu2;
 69                     result = z + "‘" + (shu1 - z * shu2) +"/" + shu2;
 70                 }
 71             }
 72             else
 73                 result = shu1 + "/" + shu2;
 74         }
 75     }
 76     
 77     public String show()
 78     {
 79         return shu1 + " " + fh + " " + shu2;
 80     }
 81     
 82     public boolean isInteger(){ 
 83         double r, s1 = shu1, s2 = shu2;
 84         if(fh == ‘/‘)
 85         {    
 86             r = s1 / s2;
 87             String str = r + "";
 88             for(int i = 0;i < str.length();i++)
 89             {
 90                 if(str.charAt(i) == ‘.‘ && i == str.length() - 2 && str.charAt(i + 1) == ‘0‘)
 91                     return true;
 92             }
 93             return false;
 94         }
 95         else
 96             return true;
 97         
 98          
 99     } 
100 }
Cal_zs.java

 

 

 

技术分享
 1 package calculat;
 2 
 3 public class shu {
 4     
 5     private Cal_zs [] c1 = new Cal_zs [100];
 6     private Cal_zfs [] c2 = new Cal_zfs [100];
 7     boolean cc;
 8     int N;
 9     int wei;
10 
11     public void setN(int n) {
12         N = n;
13     }
14 
15     public void setwei(int w)
16     {
17         wei = w;
18     }
19     public void setCc(boolean cc) {
20         this.cc = cc;
21     }
22 
23     public shu()
24     {
25         
26     }
27     
28     public shu(boolean cc, int w, boolean fs, boolean ys)
29     {
30         for(int i = 0;i < 100;i++)
31         {
32             c1[i] = new Cal_zs(cc, w);
33             while((ys == false && yu(c1[i].getResult())) || (fs == false && c1[i].getResult().charAt(0) == ‘-‘))
34             {
35                 c1[i] = new Cal_zs(cc, w);
36             }
37             for(int j = 0;j < i;j++)
38             {
39                 if(c1[i].getShu1() == c1[j].getShu1() && c1[j].getShu2() == c1[i].getShu2()&& 
40                     c1[i].getFh() == c1[j].getFh())
41                 {
42                     c1[i] = new Cal_zs(cc, w);
43                     j = 0;
44                 }
45             }
46             
47             c2[i] = new Cal_zfs(cc, w);
48             while(fs == false && c2[i].getResult().charAt(0) == ‘-‘)
49             {
50                 c2[i] = new Cal_zfs(cc, w);
51             }
52             for(int k = 0;k < i;k++)
53             {
54                 if(c2[i].getShu1().equals(c2[k].getShu1()) && c2[k].getShu2().equals(c2[i].getShu2()) && 
55                         c2[i].getFh() == c2[k].getFh())
56                 {
57                     c2[i] = new Cal_zfs(cc, w);
58                     k = 0;
59                 }
60             }
61         }
62     }
63     
64     public boolean yu(String str)
65     {
66         for(int i = 0;i < str.length(); i++)
67         {
68             if(str.charAt(i) == ‘/‘)
69                 return true;
70         }
71         return false;
72     }
73     
74     public String show1(int i)
75     {
76         return c1[i].show();
77     }
78     public String show2(int i)
79     {
80         return c2[i].show();
81     }
82 
83     public String result1(int i)
84     {
85         return c1[i].getResult() + "";
86     }
87     public String result2(int i)
88     {
89         return c2[i].getResult() + "";
90     }
91 }
shu.java

 

 

 

技术分享
 1 package com.db;
 2 
 3 import java.sql.*;
 4 
 5 public class DB {
 6     private static final String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
 7     private static final String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=calculate";
 8     private static final String dbusername = "sa";
 9     private static final String dbpassword = "4980";
10     private static Connection coon = null;
11 
12     public static Connection getCoon() {
13         return coon;
14     }
15 
16     static{
17         try{
18             Class.forName(driverStr);
19             coon = DriverManager.getConnection(connStr, dbusername, dbpassword);
20         } catch (ClassNotFoundException e){
21             e.printStackTrace();
22         } catch (SQLException e){
23             e.printStackTrace();
24         }
25     }
26     
27     public void closeCon(Connection con) {
28         if(coon!=null){
29             try {
30                 coon.close();
31             } catch (SQLException e) {
32                 e.printStackTrace();
33             }
34         }
35     }
36     
37     public static void main(String[] args) {
38         try {
39             DB.getCoon();
40             System.out.println("数据库连接成功");
41         } catch (Exception e) {
42             e.printStackTrace();
43         }
44     }
45 }
DB.java

 

 

 

 1 package servelet;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.RequestDispatcher;
 5 import javax.servlet.ServletException;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 import javax.swing.JOptionPane;
11 
12 import bean.User;
13 
14 @WebServlet("/loginservlet")
15 public class loginservlet extends HttpServlet {
16     private static final long serialVersionUID = 1L;
17        
18     public loginservlet() {
19         super();
20     }
21 
22     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
23         String user = request.getParameter("user");
24         String pass = request.getParameter("pass");
25         
26         User use = new User();
27         boolean b =use.validate(user, pass);
28         String forward;
29         
30         if(b)
31         {
32             request.getSession().setAttribute("user", user);
33             forward = "first.jsp";
34         }
35         else
36         {
37             JOptionPane.showMessageDialog(null, "用户名或密码输入有误!");  
38             forward = "login.jsp";
39         }
40         
41         
42         RequestDispatcher dispatcher = request.getRequestDispatcher(forward);
43         dispatcher.forward(request, response);
44     }
45 
46     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47         doGet(request, response);
48     }
49 
50 }

 

 

 

技术分享
 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 <script type="text/javascript">
 9     function judge(form)
10     {
11         user = form.user.value;
12         pass = form.pass.value;
13         
14         if(user == "")
15         {
16             alert("用户名为空!");
17             form.user.focus();
18             return false;
19         }
20         if(pass == "")
21         {
22             alert("密码为空!");
23             form.pass.focus();
24             return false;
25         }
26     }
27 </script>
28 <style>
29     body,p,div,ul,li,h1,h3,h4,h5,h6{
30         margin:0;
31         padding: 0;
32     }
33     h2{
34         color:red;
35         text-align: center;
36         margin:150px auto;
37     }
38     body{
39         background: #E9E9E9;
40     }
41     
42     #login{
43         width: 400px;
44         height: 250px;
45         background: #FFF;
46         margin:50px auto;
47         position: relative;
48     }
49     #login form p{
50         text-align: center;
51     }
52     
53     #user{
54         background:url(images/user.png) rgba(0,0,0,0.1) no-repeat;
55         width: 200px;
56         height: 30px;
57         border:solid #ccc 1px;
58         border-radius: 3px;
59         padding-left: 32px;
60         margin-top: 50px;
61         margin-bottom: 30px;
62     }
63     #pwd{
64         background: url(images/pwd.png) rgba(0,0,0,0.1) no-repeat;
65         width: 200px;
66         height: 30px;
67         border:solid #ccc 1px;
68         border-radius: 3px;
69         padding-left: 32px;
70         margin-bottom: 30px;
71     }
72     #submit{
73         width: 232px;
74         height: 30px;
75         background: rgba(0,0,0,0.1);
76         border:solid #ccc 1px;
77         border-radius: 3px;
78     }
79     
80     #submit:hover{
81         cursor: pointer;
82         background:#D8D8D8;
83     }
84 </style>
85 </head>
86 <body>
87     <h2>答题系统登录界面</h2>
88 <div id="login">    
89     <form name="form" action="loginservlet" method="post" onsubmit="return judge(form)">
90         <p><input type="text" name="user" id="user" placeholder="用户名"></p>
91         <p><input type="password" name="pass" id="pwd" placeholder="密码"></p>
92         <p><input type="submit" id="submit" value="http://www.mamicode.com/登录"></p>
93     </form>
94 </div>
95 </body>
96 </html>
login.jsp

 

 

 

技术分享
  1 <%@ page language="java" import="java.sql.*" contentType="text/html; charset=gb2312"
  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 <script type="text/javascript" src="http://www.mamicode.com/js/jquery-1.11.3.min.js"></script>
  8 <script type="text/javascript">
  9     $(function(){
 10         $("#chu").hide();
 11         $("#cuo").hide();
 12     
 13         $("#zhuxiao").click(function(){
 14             $(location).attr(‘href‘, ‘login.jsp‘);    
 15         });
 16         
 17         $("#chuti").click(function(){
 18             $("#chu").show();
 19             $("#cuo").hide();
 20         });
 21         
 22         $("#cuoti").click(function(){
 23             $("#cuo").show();
 24             $("#chu").hide();
 25         });
 26     });
 27 </script>
 28     <title>出题</title>
 29     <style type="text/css">
 30         h2{
 31             color:red;
 32         }
 33         #b{
 34             text-align: center;
 35             height: 60px;
 36         }
 37         body{ 
 38         background:url("images/bg.jpg")
 39         no-repeat;
 40         background-size:cover;
 41     }
 42     </style>
 43 </head>
 44 <body>
 45     <h2 align="center">出题器</h2>
 46     <h3 align="center">${sessionScope.user}您好,欢迎登录!</h3>
 47     
 48     <div id="b">
 49         <input type="button" id="chuti" value="http://www.mamicode.com/开始出题"/>
 50         <input type="button" id="cuoti" value="http://www.mamicode.com/查看历史错题"/>
 51         <input type="button" id="zhuxiao" value="http://www.mamicode.com/注销登录"/>
 52     </div>
 53     
 54     <div id="chu"> 
 55         <form name="form1" action="chuti.jsp" method="post">
 56         <table align="center">
 57             <tr>
 58                 <td>题目类型:</td>
 59                 <td>
 60                     <select name="lei">
 61                       <option value="http://www.mamicode.com/整数" selected>整数</option>
 62                       <option value="http://www.mamicode.com/真分数">真分数</option>
 63                       <option value="http://www.mamicode.com/混合">混合</option>
 64                     </select>
 65                 </td>  
 66             </tr>
 67             <tr>
 68                 <td>是否有乘除法:</td>
 69                 <td>
 70                     <input type="radio" name="cc" value="http://www.mamicode.com/无" checked> 71                     <input type="radio" name="cc" value="http://www.mamicode.com/有"> 72                 </td>
 73             </tr>
 74             <tr>
 75                 <td>是否有括号:</td>
 76                 <td>
 77                     <input type="radio" name="kh" value="http://www.mamicode.com/无" checked> 78                     <input type="radio" name="kh" value="http://www.mamicode.com/有">有 (暂未实现)
 79                 </td>
 80             </tr>
 81             <tr>
 82                 <td>数值范围:</td>
 83                 <td>
 84                     <select name="wei">
 85                       <option value="http://www.mamicode.com/一位数" selected>一位数</option>
 86                       <option value="http://www.mamicode.com/两位数">两位数</option>
 87                       <option value="http://www.mamicode.com/三位数">三位数</option>
 88                     </select>
 89                 </td>  
 90             </tr>
 91             <tr>
 92                 <td>加减有无负数:</td>
 93                 <td>
 94                     <input type="radio" name="fushu" value="http://www.mamicode.com/无" checked> 95                     <input type="radio" name="fushu" value="http://www.mamicode.com/有"> 96                 </td>
 97             </tr>
 98             <tr>
 99                 <td>除法有无余数:</td>
100                 <td>
101                     <input type="radio" name="yushu" value="http://www.mamicode.com/无" checked>102                     <input type="radio" name="yushu" value="http://www.mamicode.com/有">103                 </td>
104             </tr>
105             <tr>
106                 <td>题量</td>
107                 <td>
108                     <input type="text" name="num" value="http://www.mamicode.com/1">不大于100题
109                 </td>
110             </tr>
111             <tr>
112                 <td>
113                     <input type="hidden" name="user" value="http://www.mamicode.com/${sessionScope.user}"/>
114                 </td>
115                 <td><input type="submit" value="http://www.mamicode.com/开始出题!"></td>
116             </tr>
117         </table>
118         </form>
119     </div>
120     
121     <div id="cuo" align="center">
122     <form  action="cuo.jsp" method="post">
123         <table>
124             <th>错题题目</th>
125             <th>&nbsp;</th>
126             <th>错题答案</th>
127             <jsp:useBean id="db" class="com.db.DB" scope="request"/>
128             <%
129             String user = request.getSession().getAttribute("user").toString();
130             String sq = "select * from subject where cuo=1 and [user]=?";
131              PreparedStatement st = null;
132              st = db.getCoon().prepareStatement(sq);
133              st.setString(1, user);
134              ResultSet rs = st.executeQuery();
135             while(rs.next())
136             {
137                 out.print("<tr><td>" + rs.getString(2) + "</td><td>&nbsp;=&nbsp;</td>");
138                 out.print("<td>" + rs.getString(5) + "</td>");
139             %>
140                 </tr>
141             <%} %>
142             <tr>
143                 <td><input type="hidden" name="user" value=http://www.mamicode.com/>"color: #008080">144                 <td><input type="submit" value="http://www.mamicode.com/重新做题"></td>
145             <tr>
146         </table>
147     </form>
148     </div>
149 </body>
150 </html>
first.jsp

 

 

 

技术分享
  1 <%@ page language="java" import="java.util.*, java.sql.*, calculat.shu" contentType="text/html; charset=gb2312"
  2     pageEncoding="UTF-8"%>
  3 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
  4 <fmt:requestEncoding value="http://www.mamicode.com/gb2312"/>
  5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  6 <html>
  7 <head>
  8 <style type="text/css">
  9     h2{color:red}
 10     
 11     .tb1{
 12     border-left-style:solid;
 13     border-bottom-style:solid;
 14     border-top-style:solid;
 15     border-right-style:solid;
 16     border-width:2px;
 17     }
 18     
 19     #submit{
 20         height: 30px;
 21         background: rgba(0,0,0,0.1);
 22         border:solid #ccc 1px;
 23         border-radius: 3px;
 24     }
 25     
 26     #submit:hover{
 27         cursor: pointer;
 28         background:#D8D8D8;
 29     }
 30 </style>
 31 <script type="text/javascript">
 32   var i = 0;
 33   var Color = new Array("#0000FF", "#99FF00", "#660033", "#CC66CC", "#FFFF33");
 34   function change(){
 35       if(i > Color.length - 1)
 36           i = 0;
 37       table1.style.borderColor = Color[i];
 38       i++;
 39       setTimeout("change()", 500);
 40   }
 41 </script>
 42     <title>出题</title>
 43 </head>
 44 <body onl oad="change()">
 45     <h1 align="center">题目</h1>
 46       <table id="table1" align="center" class="tb1">
 47           <tr>
 48               <td>题目类型:</td>
 49               <td>${param.lei}</td>
 50               <td>&nbsp;&nbsp;</td>
 51               <td>是否有乘除法:</td>
 52               <td>${param.cc}</td>
 53           </tr>
 54           <tr>
 55               <td>是否有括号:</td>
 56               <td>${param.kh}</td>
 57               <td>&nbsp;&nbsp;</td>
 58               <td>数值范围:</td>
 59               <td>${param.wei}</td>
 60           </tr>
 61           <tr>
 62               <td>加减有无负数:</td>
 63               <td>${param.fushu}</td>
 64               <td>&nbsp;&nbsp;</td>
 65               <td>除法有无余数:</td>
 66               <td>${param.yushu}</td>
 67           </tr>
 68       </table>
 69       
 70       <h2 align="center">请填写答案:</h2>
 71       <form name="form2" action="result.jsp" method="post">
 72           <table align="center">
 73           <tr>
 74               <th>题目</th>
 75               <th>&nbsp;</th>
 76               <th>答案</th>
 77           </tr>
 78           
 79           <jsp:useBean id="c" class="calculat.shu" scope="request"/>
 80           <jsp:useBean id="db" class="com.db.DB" scope="request"/>
 81           <%
 82           boolean cc = true;//有无乘除,并赋值
 83           if((request.getParameter("cc")).equals("无"))
 84           {
 85               cc = false;
 86           }
 87           boolean kh = true;//有无乘除,并赋值
 88           if((request.getParameter("kh")).equals("无"))
 89           {
 90               kh = false;
 91           }
 92           int w;
 93           if(request.getParameter("wei").equals("一位数"))
 94           {
 95               w = 1;
 96           }
 97           else if(request.getParameter("wei").equals("两位数"))
 98           {
 99               w = 2;
100           }
101           else
102           {
103               w = 3;
104           }
105            
106           boolean fs = true;
107           if(request.getParameter("fushu").equals("无"))//负数
108           {
109               fs = false;
110           }
111           boolean ys = true;
112           if(request.getParameter("yushu").equals("无"))//余数
113           {
114               ys = false;
115           }
116           
117           //生成算式
118           c = new shu(cc, w, fs, ys);
119           int N = Integer.parseInt(request.getParameter("num"));//出题数
120           c.setN(N);
121           
122           String str = request.getParameter("lei");//出题类型
123           c.setCc(cc);
124          
125           //清空数据库中前N个元素
126           for(int i = 0;i < N;i++)
127           {
128               String sql="delete from subject where num=" + (i+1) + "and [user]=‘" + request.getParameter("user") +"‘";//生成一条sql语句
129               Statement stmt = db.getCoon().createStatement();
130               stmt.execute(sql);
131           }
132           
133           //生成算式,并存储在数据库中
134           String s = "", q = "";
135           for(int i = 0;i < N;i++)
136             {
137             out.print("<tr><td>");
138               if(str.equals("整数"))
139               {
140                   q = c.show1(i);
141                   out.print(q + "</td>");
142                   s = c.result1(i);
143               }
144               else if(str.equals("真分数"))
145               {
146                   q = c.show2(i);
147                   out.print(q + "</td>");
148                   s = c.result2(i);
149               }
150               else
151               {
152                   if((new Random().nextInt(10)) % 2 == 0)
153                   {
154                       q = c.show1(i);
155                       out.print(q + "</td>");
156                       s = c.result1(i);
157                   }
158                   else
159                   {
160                       q = c.show2(i);
161                       out.print(q + "</td>");
162                       s = c.result2(i);
163                   }
164               }
165               String sql = "insert into subject (num, question,answer,[user]) values (?,?,?,?)";
166               PreparedStatement pstmt = null;
167               pstmt = db.getCoon().prepareStatement(sql);
168               pstmt.setString(1, i+1+"");
169               pstmt.setString(2, q);
170               pstmt.setString(3, s);
171               pstmt.setString(4, request.getParameter("user"));
172               pstmt.executeUpdate();
173           %>
174              <td>=</td>
175              <td><input name="result<%=i %>" id="result" type="text">
176              </td></tr>
177           <%} %>
178           <tr>
179           <td>
180               <input type="hidden" name="num" value="http://www.mamicode.com/${param.num}">
181           </td>
182           <td>
183               <input type="hidden" name="user" value="http://www.mamicode.com/"/>
39           </td>
40           <td>
41               <input type="hidden" name="user" value="http://www.mamicode.com/<%=request.getParameter("user")%>"/>
42           </td>
43           </tr>
44           <tr>
45               <td></td>
46               <td></td>
47               <td><input type="submit" id="submit" value="http://www.mamicode.com/点击提交答案"></td>
48           </tr>
49           </table>
50       </form>
51 </body>
52 </html>

 

 

 

 1 <%@ page language="java" import="java.sql.*" contentType="text/html; charset=gb2312"
 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     <style type="text/css">
 9     a{
10     text-align:center;
11     color:blue;
12     }
13     </style>
14 </head>
15 <body>
16     <h1 align="center">成绩</h1>
17     <table align="center">
18     <jsp:useBean id="db" class="com.db.DB" scope="request"/>
19     <%
20         int N = Integer.parseInt(request.getParameter("num"));//出题数
21         String user = request.getParameter("user");
22         String x = "";
23         int i = 0;
24            
25            String sq = "select * from subject where cuo=1 and [user]=?";
26             PreparedStatement st = null;
27             st = db.getCoon().prepareStatement(sq);
28             st.setString(1, user);
29             ResultSet rs = st.executeQuery();
30            while(rs.next())
31            {
32                x = (String)request.getParameter("result" + i++);
33                out.print("<tr><td>" + rs.getString(2) + "</td><td>&nbsp;=&nbsp;</td>");
34                if(rs.getString(3).equals(x))
35                {
36                    out.print("<td>" + x + "</td>");
37                    %>
38                    <td><img alt="正确" src="http://www.mamicode.com/images/accept.png"></td>
39                    </tr>
40                    <%
41                      String sql="delete from subject where question=‘" + rs.getString(2) + "‘and [user]=‘" + user +"‘";//生成一条sql语句
42                   Statement stmt = db.getCoon().createStatement();
43                   stmt.execute(sql);
44                }
45                else 
46                {
47                    out.print("<td>" + x + "</td>");
48                    %>
49                    <td><img alt="错误" src="http://www.mamicode.com/images/gif-0130.gif">正确答案为<%=rs.getString(3) %></td>
50                    </tr>
51                    <%
52                    String geng = "update subject set my=? where [user]=? and question=‘" + rs.getString(2) +"‘";
53                    st = db.getCoon().prepareStatement(geng);
54                    st.setString(1, x);
55                    st.setString(2, user);
56                    st.executeUpdate();
57                }
58         }
59      %>
60     </table>
61     <div align="center">
62         <a href="http://www.mamicode.com/first.jsp">返回出题界面</a>
63     </div>
64 </body>
65 </html>

运行结果截图

 

编程总结及体会

软件工程结对作业01