标签:个数 用户 logs width 保存 link i++ ref out
1.源程序
shuru.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 background="picture/132.jpg">
<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="width:220px;height:60px;font-size:30px"></center>
</div>>
</form>
</body>
</html>
ti.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>Insert title here</title>
</head>
<body background="picture/132.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>
jieguo.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 background="picture/132.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>
2.截图
,
3.总结反思
这次我们的程序从截图上就可以看出并不是很完善,在普通运算这个等级里,结果并没有控制在100以内,还需要改进。
第二个高级运算,有了加减乘除,但是还不完善,它出题的数量不能太大,所以明天的四则运算我们会改进一下,同时这个界面的美感也需要改进,适合小朋友们使用。
标签:个数 用户 logs width 保存 link i++ ref out
原文地址:http://www.cnblogs.com/sunshine-z/p/7994530.html