一、设计思想
(1)使用css语句,让用户可以自行确定题目的数量与难度,欢迎界面加上流动字体;
(2)用户没答一题系统就进行一次正误判断,若答错则报错并显示正确答案;
(3)表达式由系统随机产生,范围由用户自行定义;
(4)多个界面用不同的jsp文件来表示,式子与答题情况都保存在数据库中。
二、源程序代码
<%@ 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"> <link href="css/NewFile.css" rel= "stylesheet"> <title>首页</title> </head> <body > <div style="position:absolute; z-index:-1;100%;height:100%;"> <img src="image/xiamu.jpg" width="100%" height="100%"/> </div> <form name="form1" method="post"action="ti.jsp"> <div id="form"> <marquee scrollamount ="5" >
<span class="STYLE1">题海欢迎您!<br> 现在时间为:<div id="time1"> id is hang_2</div><script> setInterval("document.getElementById('time1').innerHTML = new Date().toLocaleString();", 1000);</script> </marquee> <br><br><br><br><br> <center>四则运算</center> <table align="center"> <tr> <td> 请输入要出题的题目数量: </td> <td> <input name="shuliang" type="text" id="shuliang"maxlength="5"></td> </tr> <tr> <td>请选择计算的难易程度: </td> <td> <select name="leixing"> <option value=1>基础运算</option> <option value=2>高级运算</option> </select> </td> </tr> </table> <center><input type="submit"value="开始答题" style="220px;height:60px;font-size:30px"></center> </div>> </form> </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"> <link href="css/NewFile.css" rel= "stylesheet"> <title>结果</title> </head> <body background="image/xiamu.jpg"> <% String[] s= (String[])session.getAttribute("v1"); String[] answer= (String[])session.getAttribute("v2"); String[] daan=new String[s.length]; int count=0;//正确答案的个数 for(int i=0;i<s.length;i++) { out.print("运算式为:"); out.print(s[i]+" "); out.print("您的输入结果为:"); daan[i]=request.getParameter("name"+i); out.print(daan[i]+" "); out.print("正确结果为:"); out.print(answer[i]); if(Integer.parseInt(daan[i])==Integer.parseInt(answer[i])) { out.print("答对了,真棒!"); count++; } else { out.print("答错了,真遗憾!"); } out.print("<br>"); } out.print("你一共答对的题目数:"); out.print(count); %> </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"> <title>Insert title here</title> </head> <body background="image/xiamu.jpg"> <% String leixing=request.getParameter("leixing"); if(Integer.parseInt(leixing)==1) { %> <% int answer; //所出题目的答案 String j=request.getParameter("shuliang");//出的题的数量 int shuliang= Integer.valueOf(j); String[] str=new String[shuliang]; String[] daan=new String[shuliang];//用户输入的答案 String[] answer1=new String[shuliang]; //计算之后的正确答案 for(int i=0;i<shuliang;i++) { daan[i]="name"+i; //生成0-100之间的随机数 int num1=(int)(Math.random()*100); int num2=(int)(Math.random()*100); int fuhao= (int)(Math.random()*4); if(fuhao==0) { str[i]=num1+"+"+num2+"="; answer=num1+num2; } else if(fuhao==1) { str[i]=num1+"-"+num2+"="; answer=num1-num2; } else if(fuhao==2) { str[i]=num1+"*"+num2+"="; answer=num1*num2; } else { str[i]=num1+"/"+num2+"="; answer=num1/num2; } answer1[i]=""+answer; out.print(str[i]); %> <br> <form name="form2"method="post"action="jieguo.jsp"> 请输入你的答案:<input name="<%=daan[i] %>" type="text" id="daan"maxlength="5"> <br>
<% } %> <% session.setAttribute("v1",str); session.setAttribute("v2",answer1); %> <input type="submit"value="确定"> </form> <% } %>
<% if(Integer.parseInt(leixing)==2) { int answer; //所出题目的答案 String j=request.getParameter("shuliang");//出的题的数量 int shuliang= Integer.valueOf(j); String[] str=new String[shuliang]; String[] daan=new String[shuliang];//用户输入的答案 String[] answer1=new String[shuliang]; //计算之后的正确答案 for(int i=0;i<shuliang;i++) { daan[i]="name"+i; str[i]=""+(int)((Math.random()*100)); //在1-100之间随机产生一个数 int num=(int)(Math.random()*4+2); int jisuan[]=new int[num+1];//用于保存中间结果 jisuan[0]=Integer.parseInt(str[i]); char yunsuanfu[]=new char[num+1]; for(int l=1;l<num;l++) { int fuhao= (int)(Math.random()*4);//用1-4代表符号,随机产生数字 int num2=(int)(Math.random()*100); jisuan[l]=num2; if(fuhao==0) { str[i]+="+"+num2; yunsuanfu[l]='+'; } if(fuhao==1) { str[i]+="-"+num2; yunsuanfu[l]='-'; } if(fuhao==2) { str[i]+="*"+num2; yunsuanfu[l]='*'; } if(fuhao==3) { str[i]+="/"+num2; yunsuanfu[l]='/'; } } out.print(str[i]+"<br>"); for(int l=1;l<num;l++) { if(yunsuanfu[l]=='*') { jisuan[l]=jisuan[l-1]*jisuan[l]; jisuan[l-1]=0; yunsuanfu[l]=yunsuanfu[l-1]; } else if(yunsuanfu[l]=='/') { jisuan[l]=jisuan[l-1]/jisuan[l]; jisuan[l]=0; yunsuanfu[l]=yunsuanfu[l-1]; } } for(int l=1;l<num;l++) { if(yunsuanfu[l]=='+') { jisuan[l]=jisuan[l-1]+jisuan[l]; jisuan[l-1]=0; } else if(yunsuanfu[l]=='-') { jisuan[l]=jisuan[l-1]-jisuan[l]; jisuan[l-1]=0; } } answer1[i]=""+jisuan[num-1]; %> <br> <form name="form2"method="post"action="jieguo.jsp"> 请输入你的答案:<input name="<%=daan[i] %>" type="text" id="daan"maxlength="5"> <br> <% } %> <% session.setAttribute("v1",str); session.setAttribute("v2",answer1); %> <input type="submit"value="确定"> </form> <% } %>
<table> <tr> </tr> </table> </body> </html>
三、截图
四、原因分析
已完成