1》设计思路
1.首先编写一个input.jsp页面,用于输入各种参数,如出题的个数和数值上限,简单运算还是混合运算
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>首页</title>
</head>
<body background="image/beijing1.jpg">
<form name="form1" method="post"action="index.jsp">
<center>小学生的四则运算</center>
<table align="center">
<tr>
<td> 请输入要出题的题目数量: </td>
<td> <input name="shuliang" type="text" id="shuliang"maxlength="5"></td>
</tr>
<tr>
<td>
请输入要出题范围(1~?):
</td>
<td>
<input name="fanwei" type="text" id="fanwei"maxlength="5">
</td>
</tr>
<tr>
<td>请选择计算的类型:
</td>
<td>
<select name="leixing">
<option value=0>简单运算</option>
<option value=1>混合运算</option>
</select>
</td>
</tr>
</table>
<center><input type="submit"value="开始答题"></center>
</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"> <title>Insert title here</title> </head> <body background="image/beijing2.jpg"> <% String leixing=request.getParameter("leixing"); if(Integer.parseInt(leixing)==0) { %> <% 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]; //正确答案 String j1=request.getParameter("fanwei");//数值范围 int fanwei=Integer.valueOf(j1); for(int i=0;i<shuliang;i++) { daan[i]="name"+i; //生成0-100之间的随机数 int num1=(int)(Math.random()*fanwei+1); int num2=(int)(Math.random()*fanwei+1); 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="Result.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)==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]; //正确答案 String j1=request.getParameter("fanwei");//数值范围 int fanwei=Integer.valueOf(j1); for(int i=0;i<shuliang;i++) { daan[i]="name"+i; str[i]=""+(int)((Math.random()*fanwei)+1); //随机产生第一个数 int num=(int)(Math.random()*3+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);//随机产生符号 int num2=(int)(Math.random()*fanwei+1); 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="Result.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> <td><img src="image/gougou.jpg"width="70"height ="74"></td> <td>书山有路勤为径,学海无涯苦作舟!</td> </tr> </table> </body> </html>
Result.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">
<title>结果</title>
</head>
<body background="image/beijing3.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>