JAVAWEB-小学生自动出题系统
运行结果:
设置题目页面:
如果用户输入数据非法,则弹出提示框:
做题页面:
成绩单页面:
注:如果点击“再做一次”可以返回设置题目页面。
---------------------------------------------------------------------------------------------------------------分割线---------------------------------------------------------------------------------------------------------------------------------
具体实现代码如下:
设置题目页面:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>设置题目格式</title> 6 <script type="text/javascript"> 7 function check(){ /*检查用户输入的数据是否合法*/ 8 var reg=/^[1-9][0-9]*$/; 9 var flag=false; 10 if(!(reg.test(sty.sty1.value)&®.test(sty.sty2.value))) 11 { 12 alert("请输入正整数"); 13 } 14 else flag=true; 15 return flag; 16 } 17 </script> 18 <style> 19 .button { 20 background-color: #ff8433; 21 border: none; 22 color: white; 23 padding: 5px 10px; 24 text-decoration: none; 25 display: inline-block; 26 font-size: 16px; 27 margin: 2px 2px; 28 cursor: pointer; 29 } 30 #bk { 31 border: 3px solid #a1a1a1; 32 padding: 5px 2px; 33 /* background:#dddddd; */ 34 border-radius: 15px; 35 text-align: center; 36 } 37 table{ 38 margin: auto; 39 } 40 </style> 41 </head> 42 <body> 43 <br> 44 <br> 45 <br> 46 <h3 align="center" style="font-weight: bolder;font-size: xx-large;">数学考试</h3> 47 <hr> 48 <form action="temp.jsp" method="post" name="sty" onsubmit="return check(this);"> 49 <div style="text-align: center;"> 50 <table > 51 <tr> 52 <td style="text-align: left;">题目数量 </td> 53 <td><input id="bk" type="text" name="sty1" size="2" maxlength="5" ></td> 54 </tr> 55 <tr></tr> 56 <tr> 57 <td >每行题目数量</td> 58 <td><input id="bk" type="text" name="sty2" size="2" maxlength="1"></td> 59 </tr> 60 <tr></tr> 61 <tr> 62 <td colspan="2"><input class ="button" type="submit" value="确认" ></td> 63 <td colspan="2"></td> 64 </tr> 65 </table> 66 </div> 67 </form> 68 </body> 69 </html>
做题页面:
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 <style> 9 #bk { 10 border: 3px solid #a1a1a1; 11 padding: 5px 2px; 12 /* background:#dddddd; */ 13 border-radius: 15px; 14 text-align: center; 15 } 16 table{ 17 margin: auto; 18 } 19 </style> 20 </head> 21 <%!String expression=null; 22 int sum,row; 23 %> 24 <%! public int getNum() 25 { 26 return (int)(Math.random()*100)+1; 27 } 28 public int getKey() 29 { 30 return (int)(Math.random()*10)%4; 31 } 32 public int getResult(int num1,int num2,int key) 33 { 34 int sum=0; 35 switch(key) 36 { 37 case 0:sum=num1+num2;break; 38 case 1:sum=num1-num2;break; 39 case 2:sum=num1*num2;break; 40 case 3:sum=num1/num2;break; 41 default:break; 42 } 43 return sum; 44 } 45 public String getStr() 46 { 47 boolean flag=false; 48 String strsult=null; 49 String str=null; 50 while(!flag){ 51 flag=true; 52 int num1,num2,key; 53 num1=getNum(); 54 num2=getNum(); 55 key=getKey(); 56 int temp=num1; 57 num1=Math.max(num1,num2); 58 num2=Math.min(temp,num2); 59 switch(key) 60 { 61 case 0:str="+";break; 62 case 1:str="-";break; 63 case 2:{ 64 str="*"; 65 if(num1*num2>=100) 66 flag=false; 67 }break; 68 case 3:{ 69 str="/"; 70 if(num1%num2!=0) 71 flag=false; 72 }break; 73 default:break; 74 } 75 if(flag) 76 { 77 strsult=num1+str+num2+"="+getResult(num1, num2, key); 78 } 79 } 80 return strsult; 81 } 82 %> 83 <%sum=Integer.parseInt(request.getParameter("sty1")); 84 row=Integer.parseInt(request.getParameter("sty2")); 85 %> 86 <body style="text-align: center"> 87 <br> 88 <br> 89 <h1 style="text-align: center; font-size: xx-large;font-weight: bolder;">数学题</h1> 90 <hr> 91 <br> 92 <form action="theresult.jsp" method="get"> 93 <table> 94 <%for(int i=1;i<=sum/row;i++) {%> 95 <tr> 96 <%for(int j=1;j<=row;j++) {%> 97 <td style="text-align:left;" width="100px"> 98 <%=(j+(i-1)*row)%> 99 、 100 <%expression=getStr(); %> 101 <%=expression.substring(0,expression.split("=")[0].length()+1)%> 102 </td> 103 <td> 104 <input id="bk" name="tempresult" type="text" size="2" maxlength="3" style="margin-right: 30px" > 105 <input type="hidden" name="result" value=<%=expression.split("=")[1] %>> 106 </td> 107 <%}} %> 108 </tr> 109 110 111 <tr > 112 <%for(int j=1;j<=sum%row;j++) {%> 113 <td style="text-align: left;" width="100px"> 114 <%=(j+sum/row*row)%> 115 、 116 <%expression=getStr(); %> 117 <%=expression.substring(0,expression.split("=")[0].length()+1)%> 118 </td> 119 <td> 120 <input id="bk" name="tempresult" type="text" size="2" maxlength="3" style="margin-right: 30px" > 121 <input type="hidden" name="result" value=<%=expression.split("=")[1] %>> 122 </td> 123 <%} %> 124 </tr> 125 </table> 126 <br> 127 <input type="submit" value="提交"> 128 </form> 129 </body> 130 </html>
成绩单页面:
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html > 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>成绩单</title> 8 </head> 9 <body style="text-align: center;"> 10 <br> 11 <br> 12 <h1 style="text-align: center; font-size: xx-large;font-weight: bolder;">成绩</h1> 13 <hr> 14 <br> 15 <% 16 String []names=request.getParameterValues("tempresult"); 17 String results[]=request.getParameterValues("result"); 18 double count=0; 19 for(int i=0;i<results.length;i++) 20 if(results[i].equals(names[i])) 21 count++; 22 out.print("您的分数:"+count+"<br>"+"正确率:"+Math.floor(Math.round((count/results.length)*100))+"%"); 23 out.print("<br>"); 24 %> 25 <a href="init.html" style="color: blue;">再做一次</a> 26 </body> 27 </html>