一、用户故事
小学生家长需要一款软件,能够一次出10道100以内的正整数加减运算。
二、需求分析
1、随机出题,不能超纲
2、不能产生结果为负数
3、自动阅卷
4、计时器
三、具体设计
四、核心代码
Great接口
1 package cn.dao; 2 3 import cn.entity.Ti; 4 5 public interface Great { 6 public Ti OneGreat(Ti ti); 7 public void TwoGreat(); 8 public void ThreeGreat(); 9 }
Ti类:保存题目、答案
1 package cn.entity; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class Ti { 7 private int number1,number2,symbolIndex; 8 private static Map<Integer, String> TiMuMap = new HashMap<Integer, String>();//题目 9 private static Map<Integer, Integer> DaAnMap = new HashMap<Integer, Integer>();//答案 10 public void setDaAnMap(Map<Integer, Integer> daAnMap) { 11 DaAnMap = daAnMap; 12 } 13 public int getNumber1() { 14 return number1; 15 } 16 public void setNumber1(int number1) { 17 this.number1 = number1; 18 } 19 public int getNumber2() { 20 return number2; 21 } 22 public void setNumber2(int number2) { 23 this.number2 = number2; 24 } 25 public Map<Integer, String> getTiMuMap() { 26 return TiMuMap; 27 } 28 public void setTiMuMap(Map<Integer, String> tiMuMap) { 29 TiMuMap = tiMuMap; 30 } 31 public int getSymbolIndex() { 32 return symbolIndex; 33 } 34 public void setSymbolIndex(int symbolIndex) { 35 this.symbolIndex = symbolIndex; 36 } 37 38 public Map<Integer, Integer> getDaAnMap() { 39 return DaAnMap; 40 } 41 public Ti(Map<Integer, String> tiMuMap, Map<Integer, Integer> daAnMap) { 42 super(); 43 TiMuMap = tiMuMap; 44 DaAnMap = daAnMap; 45 } 46 public Ti() {} 47 @Override 48 public String toString() { 49 return "Ti [number1=" + number1 + ", number2=" + number2 + ", symbolIndex=" + symbolIndex + ", TiMuMap=" 50 + TiMuMap + ", DaAnMap=" + DaAnMap + "]"; 51 } 52 53 }
JiSuan类:写各个年级的出题规范
1 package cn.impl; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import cn.dao.Great; 7 import cn.entity.Ti; 8 9 public class JiSuan implements Great { 10 Map<Integer, String> tMap = new HashMap<Integer, String>(); 11 Map<Integer, Integer> dMap = new HashMap<Integer, Integer>(); 12 String[] symbol = { "+", "-", "*", "/" }; 13 //一年级出题形式 14 @Override 15 public Ti OneGreat(Ti ti) { 16 System.out.println("OneGreat()..."); 17 int i = 1; 18 while (true) { 19 // 随机生成两个数字 20 ti.setNumber1((int) (Math.random() * 101)); 21 ti.setNumber2((int) (Math.random() * 101)); 22 ti.setSymbolIndex((int) (Math.random() * 2)); 23 24 if (ti.getNumber1() >= ti.getNumber2() && ti.getSymbolIndex() == 1 && ti.getNumber2() != 0) { 25 tMap.put(i, ti.getNumber1() + symbol[ti.getSymbolIndex()] + ti.getNumber2() + "="); 26 dMap.put(i, ti.getNumber1() - ti.getNumber2()); 27 i++; 28 } else if (ti.getSymbolIndex() == 0 && ti.getNumber1() + ti.getNumber2() <= 100 && ti.getNumber1() != 0 29 && ti.getNumber2() != 0) { 30 tMap.put(i, ti.getNumber1() + symbol[ti.getSymbolIndex()] + ti.getNumber2() + "="); 31 dMap.put(i, ti.getNumber1() + ti.getNumber2()); 32 i++; 33 } 34 if (i == 11) { 35 ti.setTiMuMap(tMap); 36 ti.setDaAnMap(dMap); 37 return ti; 38 } 39 } 40 } 41 //二年级出题形式 42 @Override 43 public void TwoGreat() { 44 // TODO Auto-generated method stub 45 46 } 47 //三年级出题形式 48 @Override 49 public void ThreeGreat() { 50 // TODO Auto-generated method stub 51 52 } 53 54 }
begin.jsp:开始界面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h1>登录</h1> 11 <form action="/CalculateTheMouthCard/First" method="post"> 12 <select name="great"> 13 <option>一年级</option> 14 <option>二年级</option> 15 </select> 16 <p><input type="submit" value="登录"></p> 17 </form> 18 </body> 19 </html>
Fiest类:得到年级从而到达指定出题规范
1 package cn.itcast; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import cn.entity.Ti; 10 import cn.impl.JiSuan; 11 12 public class First extends HttpServlet { 13 private static final long serialVersionUID = 1L; 14 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 request.setCharacterEncoding("utf-8"); 16 String great = request.getParameter("great"); 17 if (great.equals("一年级")) { 18 response.sendRedirect("/CalculateTheMouthCard/One"); 19 } 20 21 } 22 23 /** 24 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 25 */ 26 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 27 // TODO Auto-generated method stub 28 doGet(request, response); 29 } 30 31 }
One类:将题传至前端
1 package cn.tools; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.Map; 6 import java.util.Map.Entry; 7 8 import javax.servlet.RequestDispatcher; 9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 import cn.entity.Ti; 15 import cn.impl.JiSuan; 16 17 public class One extends HttpServlet { 18 private static final long serialVersionUID = 1L; 19 20 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 21 Ti ti = new Ti(); 22 JiSuan jiSuan = new JiSuan(); 23 Ti oneGreat = jiSuan.OneGreat(ti); 24 Map<Integer, String> tiMuMap = oneGreat.getTiMuMap(); 25 Map<Integer, Integer> daAnMap = oneGreat.getDaAnMap(); 26 System.out.println("One:"+tiMuMap); 27 System.out.println("One"+daAnMap); 28 response.setContentType("text/html;charset=utf-8"); 29 PrintWriter out = response.getWriter(); 30 //将题目传至前端 31 for(Entry<Integer, String> entry : tiMuMap.entrySet()) { 32 Integer mapKey = entry.getKey(); 33 String mapValue = entry.getValue(); 34 request.setAttribute("t"+mapKey, mapValue); 35 } 36 RequestDispatcher dispatcher = request.getRequestDispatcher("/One.jsp"); 37 dispatcher.forward(request, response); 38 out.close(); 39 } 40 41 /** 42 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 43 */ 44 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 45 // TODO Auto-generated method stub 46 doGet(request, response); 47 } 48 49 }
One.jsp:出题界面
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>一年级</title> 8 <script type="text/javascript" src="js/ajax.js"></script> 9 <script type="text/javascript" src="js/jquery-3.5.1.js"></script> 10 <script type="text/javascript"> 11 //计时器 12 var shijian = self.setInterval("clock()", 1000); 13 var t = 0; 14 function clock() { 15 t++; 16 document.getElementById("clock").value = t; 17 } 18 19 //ajax判断对错 20 function light() { 21 $("#btn").attr("disabled","disabled");//设置阅卷按钮只能点击一次 22 var xhr = getXhr(); 23 var can = "?clock="+$("#clock").val(); 24 for (var i = 1; i <= 10; i++) { 25 if($("#d"+i).val() == ""){//判断学生是否答题,没有答题为k(空) 26 can = can+",k"; 27 }else{ 28 can = can+','+$("#d"+i).val() 29 } 30 } 31 var uri="Light.do"+can; 32 xhr.open("get", uri, true); 33 xhr.onreadystatechange = function() { 34 if (xhr.readyState == 4 && xhr.status == 200) { 35 var accuracy = 0;//正确率 36 for (var i = 1; i <= 10; i++) { 37 var txt = xhr.responseText; 38 $("#s"+i).append('<img src="image/'+txt[i-1]+'.png" />'); 39 $("#d"+i).attr("readonly","readonly");//阅卷后为只读 40 //查看正确率 41 if(txt[i-1] == "y"){ 42 accuracy += 10; 43 } 44 } 45 $("#accuracy").val(accuracy); 46 }; 47 }; 48 xhr.send(null); 49 } 50 </script> 51 <style type="text/css"> 52 .a { 53 width: 100px; 54 height: 30px; 55 margin-left: 10px; 56 } 57 58 .box { 59 width: 300px; 60 } 61 62 .time { 63 width: 50px; 64 height: 20px; 65 font-size: 16px; 66 text-align: center; 67 } 68 69 .xbox { 70 display: block; 71 width: 10px; 72 height: 10px; 73 border: 1px red solid; 74 } 75 span{ 76 margin-left: 20px; 77 } 78 </style> 79 </head> 80 <body> 81 <div class="box"> 82 <form action="begin.jsp" method="get"> 83 <p> 84 计时器:<input type="text" readonly id="clock" class="time" name="time">秒 85 </p> 86 <p> 87 1、${t1 }<input type="text" class="a" id="d1"><span id="s1"></span> 88 </p> 89 <p> 90 2、${t2 }<input type="text" class="a" id="d2"><span id="s2"></span> 91 </p> 92 <p> 93 3、${t3 }<input type="text" class="a" id="d3"><span id="s3"></span> 94 </p> 95 <p> 96 4、${t4 }<input type="text" class="a" id="d4"><span id="s4"></span> 97 </p> 98 <p> 99 5、${t5 }<input type="text" class="a" id="d5"><span id="s5"></span> 100 </p> 101 <p> 102 6、${t6 }<input type="text" class="a" id="d6"><span id="s6"></span> 103 </p> 104 <p> 105 7、${t7 }<input type="text" class="a" id="d7"><span id="s7"></span> 106 </p> 107 <p> 108 8、${t8 }<input type="text" class="a" id="d8"><span id="s8"></span> 109 </p> 110 <p> 111 9、${t9 }<input type="text" class="a" id="d9"><span id="s9"></span> 112 </p> 113 <p> 114 10、${t10 }<input type="text" class="a" id="d10"><span 115 id="s10"></span> 116 </p> 117 <p>正确率:<input type="text" id="accuracy" class="time" value="">%</p> 118 <input type="button" id="btn" value="阅卷" onclick="javascript:light();shijian=window.clearInterval(shijian);"> 119 <input type="submit" value="提交"> 120 </form> 121 </div> 122 </body> 123 </html>
Light类:获得学生答案,判断对错,传至前端
1 package cn.tools; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.List; 8 import java.util.Map; 9 import java.util.Map.Entry; 10 import java.util.Set; 11 12 import javax.servlet.ServletException; 13 import javax.servlet.http.HttpServlet; 14 import javax.servlet.http.HttpServletRequest; 15 import javax.servlet.http.HttpServletResponse; 16 17 import cn.entity.Ti; 18 19 /** 20 * 控制题目对错的展示 21 */ 22 public class Light extends HttpServlet { 23 24 @Override 25 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 26 System.out.println("service()..."); 27 resp.setContentType("text/html;charset=utf-8"); 28 PrintWriter out = resp.getWriter(); 29 String uri = req.getRequestURI(); 30 String str = "";//保存学生答案的对错 31 String action = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf(".")); 32 if ("/Light".equals(action)) { 33 // 获得学生答案 34 String clock = req.getParameter("clock"); 35 System.out.println("学生答案:"+clock); 36 String[] split = clock.split(","); 37 System.out.println("时间:"+split[0]); 38 // 获得正确答案 39 Ti ti = new Ti(); 40 Map<Integer, Integer> daAnMap = ti.getDaAnMap(); 41 for (Entry<Integer, Integer> entry : daAnMap.entrySet()) { 42 Integer daAnKey = entry.getKey(); 43 Integer daAnValue = entry.getValue();// 正确答案 44 System.out.println(daAnKey); 45 if (split[daAnKey].equals("k") || !Character.isDigit(split[daAnKey].charAt(0))) { 46 str = str+"n"; 47 }else { 48 int parseInt = Integer.parseInt(split[daAnKey]); 49 if (parseInt == daAnValue) { 50 str = str + "y"; 51 }else { 52 str = str + "n"; 53 } 54 } 55 } 56 System.out.println(str); 57 out.println(str); 58 } 59 out.close(); 60 } 61 62 }
测试结果
五、项目运行
六、PSP(个人软件开发流程)
七、总结
1、没有继续扩展更高年级的题型规范
2、加深了对Java Web的理解