简单的四则运算网页版

四、源代码

欢迎界面

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 2 <html>
 3 <head>
 4 <%@ page language="java" contentType="text/html; charset=UTF-8"
 5     pageEncoding="UTF-8"%>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>小学二年级算式计算</title>
 8 </head>
 9 <body>
10 <h2 align="center" style="color:red">小学二年级算式计算</h2>
11 <p align="center">本页仅供二年级的小同学测试使用,共60道100以内加减乘除算式计算题,计时8分钟。</p>
12 <div align="center">
13     <a href="proshow.jsp">按下此键开始</a>
14 </div>
15 </body>
16 </html>

题目展示界面

 1 <%@page import="CC.GradeTwo" %>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4 <head>
 5 <%@ page language="java" contentType="text/html; charset=UTF-8"
 6     pageEncoding="UTF-8"%>
 7 <meta http-equiv="refresh" content="text/html; charset=UTF-8">
 8 <script type="text/javascript">
 9       var maxtime = 8*60;
10       function CountDowm(){
11           if(maxtime>=0){
12               minutes = Math.floor(maxtime/60);
13               seconds = Math.floor(maxtime%60);
14               if(seconds>=10)
15                   msg = "剩余时间:"+minutes+":"+seconds;
16               else
17                   msg = "剩余时间:"+minutes+":0"+seconds;
18               document.all["timer"].innerHTML = msg;
19               --maxtime;
20           }
21           else{
22               document.forms("form1").submit();
23           }
24       }
25       timer = setInterval("CountDowm()",1000);
26 </script>
27 <title>小学二年级算式计算</title>
28 </head>
29 <%
30      String str = null;
31 %>
32 <body>
33 
34 <%
35     request.setCharacterEncoding("utf-8");
36 %>
37 <jsp:include flush="true" page="inc.jsp">
38    <jsp:param name="color" value="blue" />
39    <jsp:param name="title" value="小学二年级" />
40    <jsp:param name="body" value="数学口算题" />
41 </jsp:include>
42 <div id="timer"></div>
43 <form name="form1" action="${pageContext.request.contextPath}/Servlet/GradeServlet?method=end" target="_self" method="post">
44       <table align="center" border = "1">
45       <tr><th colspan="3">共60题,注意用时</th></tr>
46           <%    GradeTwo.clear();
47                 for(int i = 1;i<=60;++i)      {    
48                     str = GradeTwo.getPoly();
49 
50       %>
51       <tr><td><%=i %></td><td><%=str %></td><td><input type="text" name="list"></td></tr>
52       <%
53                 }
54       %>
55       <tr><td colspan="3" align="center"><input type="submit" value="提交"></td></tr>
56       </table>
57 </form>
58 </body>
59 </html>

结果展示界面

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 2 <html>
 3 <head>
 4 <%@page import="CC.GradeTwo" %>
 5 <%@ page language="java" contentType="text/html; charset=UTF-8"
 6     pageEncoding="UTF-8"%>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>答案</title>
 9 </head>
10 <body>
11 <h3 align="center" style="color:red">答题结果</h3>
12 <%
13    int[]end = (int[])(request.getAttribute("result"));
14    String str[] = (String[])request.getAttribute("result1");
15 %>
16 <table align="center" border="1">
17     <tr><th>答对:</th><td><%=end[0] %></td></tr>
18     <tr><th>答错:</th><td><%=end[1] %></td></tr>
19     <tr><th>未答:</th><td><%=end[2] %></td></tr>
20 </table><p>
21 <p align="center" style="color:red">答题情况</p>
22 <table align="center" border="1">
23    <tr><td>题目</td><td>提交答案</td><td>正确答案</td></tr>
24    <%   
25        String color = "";
26        for(int i = 0;i<60;++i){
27           if(GradeTwo.key.get(i).equals(str[i])){
28    %>
29        <tr><td><%=GradeTwo.pro.get(i) %></td><td style="color:green"><%=str[i] %></td><td><%=GradeTwo.key.get(i) %></td></tr>
30    <%}
31           else{
32    %>
33    <tr><td><%=GradeTwo.pro.get(i) %></td><td style="color:red"><%=str[i] %></td><td><%=GradeTwo.key.get(i) %></td></tr>
34    
35    <%
36    }
37        }
38    %>
39 </table>
40 <div align="center"><input type="button" value="再来一次" onclick="window.location.href='<%=request.getContextPath()%>/proshow.jsp'" /></div>
41 </body>
42 </html>

标头:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 4 <table width="320" bordercolor="<%=request.getParameter("color") %>" border="1" cellpadding="4" cellspacing="0">
 5 
 6 <tr bgcolor="<%=request.getParameter("color") %>" color="#ffffff">
 7 <td><%=request.getParameter("title") %></td>
 8 </tr>
 9 <tr><td valign="top"><%=request.getParameter("body") %></td></tr>
10 </table>

产生表达式的Java文件:

  1 package CC;
  2 
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 import java.util.Random;
  6 import java.util.Set;
  7 import java.util.Vector;
  8 
  9 public class GradeTwo {
 10     private static String opera = "+-*/";
 11     public static Vector<String> pro = new Vector<String>();
 12     public static Vector<String> key = new Vector<String>();
 13     public static void clear() {
 14         pro.clear();
 15         key.clear();
 16     }
 17     public static int isInteger(int c1, int c2, char opera) {
 18         int result = 0;
 19         int result1 = 0;
 20         switch (opera) {
 21         case '+':
 22             result = c1 + c2;
 23             break;
 24         case '-':
 25             result = c1 - c2;
 26             break;
 27         case '*':
 28             result = c1 * c2;
 29             break;
 30         case '/':
 31             result = c1 / c2;
 32             result1 = c1 % c2;
 33             break;
 34         }
 35         if (result >= 0 && result <= 100 && result1 == 0)
 36             return result;
 37         else
 38             return -1;
 39     }
 40 
 41     public static String getPoly() {
 42         String poly = "";
 43 //        Random random = new Random();
 44 //        
 45 //        int c1;
 46 //        int c2;
 47 //        int result;
 48 //        char operac;
 49 //        boolean flag = true;
 50 //        do {
 51 //            c1 = random.nextInt(100) + 1;
 52 //            c2 = random.nextInt(100) + 1;
 53 //            operac = opera.charAt(random.nextInt(4));
 54 //            if (operac == '*')
 55 //                poly = c1 + " × " + c2 + " = ";
 56 //            if (operac == '/')
 57 //                poly = c1 + " ÷ " + c2 + " = ";
 58 //            if (operac == '+')
 59 //                poly = c1 + " + " + c2 + " = ";
 60 //            if (operac == '-')
 61 //                poly = c1 + " - " + c2 + " = ";
 62 //            /*for (int i = 0;i<pro.size();++i) {
 63 //                if (poly.equals(pro.get(i))) {
 64 //                    flag = false;
 65 //                    break;
 66 //                }
 67 //            }*/
 68 //        } while (!flag || (result = isInteger(c1, c2, operac)) < 0);
 69 //        pro.add(poly);
 70 //        key.add(String.valueOf(result));
 71         Random random = new Random();
 72         //产生0.9的两位运算,0.1的三位运算
 73         char operate;
 74         char operate1;
 75         int operand1 = 0;
 76         int operand2 = 0;
 77         int operand3 = 0;
 78         int result;
 79         int percent = random.nextInt(100)+1;
 80         if(percent<=90) {
 81             operate = opera.charAt(random.nextInt(4));
 82             switch(operate) {
 83                case '+':
 84                    operand1 = random.nextInt(100);
 85                    operand2 = random.nextInt(100-operand1);
 86                    poly = operand1+" + "+operand2+" = ";
 87                    break;
 88                case '-':
 89                    operand1 = random.nextInt(100);
 90                    operand2 = random.nextInt(operand1+1);
 91                    poly = operand1+" - "+operand2+ " = ";
 92                    break;
 93                case '*':
 94                    operand1 = random.nextInt(9)+1;
 95                    operand2 = random.nextInt(9)+1;
 96                    poly = operand1+" × "+operand2+ " = ";
 97                    break;
 98                case '/':
 99                    operand1 = random.nextInt(9)+1;
100                    operand2 = random.nextInt(9)+1;
101                    operand1 = operand1*operand2;
102                    poly = operand1+" ÷ "+operand2+ " = ";
103                    break;
104             }
105             result = isInteger(operand1, operand2, operate);
106             key.add(String.valueOf(result));
107         }else {
108             operate = opera.charAt(random.nextInt(2));
109             operate1 = opera.charAt(random.nextInt(2));
110             if(operate=='+') {
111                 operand1 = random.nextInt(100);
112                 operand2 = random.nextInt(100-operand1);
113                 if(operate1=='+') {
114                     operand3 = random.nextInt(100-operand1-operand2);
115                     poly = operand1+" + "+operand2+" + "+operand3+" = ";
116                 }else {
117                     operand3 = random.nextInt(operand1+operand2);
118                     poly = operand1+" + "+operand2+" - "+operand3+" = ";
119                 }
120             }else {
121                 operand1 = random.nextInt(100)+1;
122                 operand2 = random.nextInt(operand1);
123                 if(operate1=='+') {
124                     operand3 = random.nextInt(100-operand1+operand2);
125                     poly = operand1+" - "+operand2+" + "+operand3+" = ";
126                 }else {
127                     operand3 = random.nextInt(operand1-operand2);
128                     poly = operand1+" - "+operand2+" - "+operand3+" = ";
129                 }
130             }
131             result = isInteger(operand1, operand2, operate);
132             result = isInteger(result, operand3, operate1);
133             key.add(String.valueOf(result));
134         }
135         pro.add(poly);
136         return poly;
137     }
138     public static int[]end(String[]list){
139         int[]result = new int[3];
140         result[0] = 0;
141         result[1] = 0;
142         result[2] = 0;
143         int len = list.length;
144         for(int i = 0;i<len;++i) {
145             if(key.get(i).equals(list[i]))result[0]++;
146             else {
147                 if(list[i].equals(""))result[2]++;
148                 else result[1]++;
149             }
150         }
151         return result;
152     }
153     public static void main(String[]args) {
154         int result = isInteger(55,13,'-');
155         result = isInteger(result,19,'-');
156         System.out.println(result);
157     }
158 }

Servlet:

 1 package Servlet;
 2 
 3 import CC.GradeTwo;
 4 import javax.servlet.http.HttpServlet;
 5 import java.io.IOException;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 
11 
12 public class GradeServlet extends HttpServlet{
13     public void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
14         String method = req.getParameter("method");
15         if(method.equals("end"))end(req,resp); 
16     }
17     public void end(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
18            String[]keys = req.getParameterValues("list");
19            req.setAttribute("result",GradeTwo.end(keys));
20            for(int i = 0;i<keys.length;++i)
21                if(keys[i].equals(""))keys[i] = "未答";
22            req.setAttribute("result1",keys);
23            req.getRequestDispatcher("../result.jsp").forward(req, resp);
24            return;
25     }
26 }

Filter:防止直接进入结果界面:

 1 package Servlet;
 2 
 3 import javax.servlet.http.HttpServlet;
 4 import java.io.IOException;
 5 
 6 import javax.servlet.Filter;
 7 import javax.servlet.FilterChain;
 8 import javax.servlet.FilterConfig;
 9 import javax.servlet.ServletException;
10 import javax.servlet.ServletRequest;
11 import javax.servlet.ServletResponse;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 
15 public class KeyFilter implements Filter{
16     protected FilterConfig filterConfig;
17     @Override
18     public void destroy() {
19         // TODO 自动生成的方法存根
20         filterConfig = null;
21     }
22 
23     @Override
24     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
25             throws IOException, ServletException {
26         // TODO 自动生成的方法存根
27           HttpServletRequest req = (HttpServletRequest)request;
28           HttpServletResponse resp = (HttpServletResponse)response;
29           String reqURL = req.getServletPath();
30           if(reqURL.equals("/result.jsp")) {
31               resp.sendRedirect("index.jsp");
32               return;
33           }
34           chain.doFilter(request,response);
35     }
36 
37     @Override
38     public void init(FilterConfig filterConfig) throws ServletException {
39         // TODO 自动生成的方法存根
40          this.filterConfig = filterConfig;
41     }
42      
43 }

xml文件:(需要添加)

 1   <display-name>GradeCal</display-name>
 2   <servlet>
 3       <servlet-name>GradeServlet</servlet-name>
 4       <servlet-class>Servlet.GradeServlet</servlet-class>
 5   </servlet>
 6   <servlet-mapping>
 7       <servlet-name>GradeServlet</servlet-name>
 8       <url-pattern>/Servlet/GradeServlet</url-pattern>
 9   </servlet-mapping>
10   <filter>
11   <filter-name>KeyFilter</filter-name>
12   <filter-class>Servlet.KeyFilter</filter-class>
13   </filter>
14   <filter-mapping>
15   <filter-name>KeyFilter</filter-name>
16   <url-pattern>/*</url-pattern>
17   </filter-mapping>

 

------------------------------------------------------------------------

END

posted @ 2017-12-06 18:56  messi2017  阅读(446)  评论(0编辑  收藏  举报