设计思想:首先在类里随机设定四则运算数,用do while循环,然后在里面用switch判断运算符,并分配相应的算法,类里有出题函数、查重函数,以及错题收集函数,然后在JSP界面上输入相应的输入题目数和时间,时间用JavaScript书写,跳转界面,达到效果
源代码:
arithmetic.java
package com.jaovo.msg.model;
public class arithmetic {
public int []answer;//答案
public int shumu;//出题数目
public String []suanshi;//算式
public String []error;//错题
public int shumuerror=0;//错题数目
public void setsuanshi(String []suanshi)
{
this.suanshi=suanshi;
}
public void seterror(String []error)
{
this.error=error;
}
public void setshumuerror(int shumuerror)
{
this.shumuerror=shumuerror;
}
public String [] biaodashi(int n)
{
shumu=n;
answer=new int[n];
int a,b,c,d1 = 0,d,d2=0;
int []mixture=new int[2];
String []biaodashi=new String[n];
for(int i=0;i<n;i++)
{
a=(int)(Math.random()*100)+1 ;//1-100
b=(int)(Math.random()*100)+1;
c=(int)(Math.random()*5)+1 ;//随机生成一个1-5的整数,4表示加法,1表示减法,2表示乘法,3表示除法,5表示混合
if(c==5)//混合运算
{
do
{
for(int m=0;m<2;m++)
{
mixture[m]=(int)(Math.random()*2);//0-1
} //控制运算符
a=(int)(Math.random()*100)+1;
b=(int)(Math.random()*100)+1;
d=(int)(Math.random()*100)+1;//生成三个数
if(mixture[0]==0&&mixture[1]==0)
{
biaodashi[i]=a+"+"+b+"+"+d+" = ";
d1=a+b+d;
}
if(mixture[0]==1&&mixture[1]==1)
{
biaodashi[i]=a+"-"+b+"-"+d+" = ";
d2=a-b;
d1=a-b-d;
}
if(mixture[0]==0&&mixture[1]==1)
{
biaodashi[i]=a+"+"+b+"-"+d+" = ";
d1=a+b-d;
}
if(mixture[0]==1&&mixture[1]==0)
{
biaodashi[i]=a+"-"+b+"+"+d+" = ";
d2=a-b;
d1=a-b+d;
}
} while(d2<0||d1<0);
answer[i]=d1;
}
if(c==4)//单加法
{
d1=a+b;
biaodashi[i]=a+"+"+b+" = ";
while(d1>100)
{
a=(int)(Math.random()*100)+1;
b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99
d1=a+b;
}
biaodashi[i]=a+"+"+b+" = ";
answer[i]=d1;
System.out.print(a+"+"+b+"= ");
}
if(c==1)//单减法
{
d1=a-b;
while(d1<0)
{
a=(int)(Math.random()*100)+1;
b=(int)(Math.random()*100)+1;
d1=a-b;
}
biaodashi[i]=a+"-"+b+" = ";
answer[i]=d1;
System.out.print(a+"-"+b+"= ");
}
if(c==2)//乘法
{
a=(int)(Math.random()*10);//0-9
b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99
d1=a*b;
while(a<1||b<1||d1>81)
{
a=(int)(Math.random()*10);//0-9
b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99
}
d1=a*b;
biaodashi[i]=a+"*"+b+" = ";
answer[i]=d1;
System.out.print(a+"*"+b+"= ");
}
if(c==3)//除法
{
d1=a/b;
while(a%b!=0||a/b>9||(a<=81&&b>=10)||(a>9&&a==b)||(a>81))
{
a=(int)(Math.random()*100)+1;
b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99
}
d1=a/b;
biaodashi[i]=a+"÷"+b+" = ";
answer[i]=d1;
System.out.print(a+"÷"+b+"= ");
}
//查重
for(int k=i-1;k>=0;k--)
{
while(biaodashi[i].equals(biaodashi[k]))
{
a=(int)(Math.random()*100)+1;//1-100
b=(int)(Math.random()*100)+1;
c=(int)(Math.random()*5)+1;//随机生成一个1-5的整数,4表示加法,1表示减法,2表示乘法,3表示除法,5表示混合
if(c==5)
{
do//混合运算
{
for(int m=0;m<2;m++)
{
mixture[m]=(int)(Math.random()*2);//0-1
} //控制运算符
a=(int)(Math.random()*100)+1;
b=(int)(Math.random()*100)+1;
d=(int)(Math.random()*100)+1;//生成三个数
if(mixture[0]==0&&mixture[1]==0)
{
biaodashi[i]=a+"+"+b+"+"+d+" = ";
d1=a+b+d;
}
if(mixture[0]==1&&mixture[1]==1)
{
biaodashi[i]=a+"-"+b+"-"+d+" = ";
d2=a-b;
d1=a-b-d;
}
if(mixture[0]==0&&mixture[1]==1)
{
biaodashi[i]=a+"+"+b+"-"+d+" = ";
d1=a+b-d;
}
if(mixture[0]==1&&mixture[1]==0)
{
biaodashi[i]=a+"-"+b+"+"+d+" = ";
d2=a-b;
d1=a-b+d;
}
}while(d2<0||d1<0);
answer[i]=d1;
}
if(c==4)
{
d1=a+b;
biaodashi[i]=a+"+"+b+" = ";
while(d1>100)
{
a=(int)(Math.random()*100)+1;
b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99
d1=a+b;
}
biaodashi[i]=a+"+"+b+" = ";
answer[i]=d1;
System.out.print(a+"+"+b+"= ");
}
if(c==1)
{
d1=a-b;
while(d1<0)
{
a=(int)(Math.random()*100)+1;
b=(int)(Math.random()*100)+1;
d1=a-b;
}
biaodashi[i]=a+"-"+b+" = ";
answer[i]=d1;
System.out.print(a+"-"+b+"= ");
}
if(c==2)
{
a=(int)(Math.random()*10);//0-9
b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99
d1=a*b;
while(a<1||b<1||d1>81)
{
a=(int)(Math.random()*10);//0-9
b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99
}
d1=a*b;
biaodashi[i]=a+"*"+b+" = ";
answer[i]=d1;
System.out.print(a+"*"+b+"= ");
}
if(c==3)
{
while(a%b!=0)
{
a=(int)(Math.random()*100)+1;
b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99
}
d1=a/b;
biaodashi[i]=a+"÷"+b+" = ";
answer[i]=d1;
System.out.print(a+"÷"+b+"= ");
}
}
}
}
return biaodashi;
}
}
Chuti,jsp
<%@page import="com.jaovo.msg.model.arithmetic"%>
<%@ page import="javax.swing.*" %>
<%@ 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><title>出题页</title></head>
<body style="background:url(C:/Users/Administrator/Pictures/任务图片/28.png)" onload="load()">
<%
//接收客户端传递过来的参数
request.setCharacterEncoding("UTF-8");
String time = request.getParameter("usertime");//接收时间
int time1=0;
int x=1;
for(int m=0;m<time.length();m++)
{
time1+=(time.charAt(time.length()-m-1)-'0')*x;
x*=10;
} //字符串类型的数字转换为整型 成为参数
%>
<script>
var c=1;
var t;
var num1=<%=time1%>
function timeCount()
{
document.getElementById("txt").innerHTML=num1-c;
c=c+1;
t=setTimeout("timeCount()",1000);
if(num1==c-1)
{
clearTimeout(t);
alert("时间到了!");
load();
}
}
function load()
{
document.getElementById("anniu").click();
}
window.onload =function(){
timeCount();//onload 事件会在页面或图像加载完成后立即发生。
}
</script>
<div align="center">
<h1 style="font-family:华文新魏;font-size:4em" >开始答题</h1>
<td style="font-family:华文新魏;font-size:1em;500px" align="right">倒计时:</td>
<p id = "txt" ></p>
<table border="1" >
<form action="Result.jsp" method="get">
<%
//接收客户端传递过来的参数
request.setCharacterEncoding("UTF-8");
String num = request.getParameter("username");//接收出题的数目
int num1=0;
x=1;
for(int m=0;m<num.length();m++)
{
num1+=(num.charAt(num.length()-m-1)-'0')*x;
x*=10;
}//字符串类型的数字转换为整型 成为参数
arithmetic demo=new arithmetic();//定义对象
String []biaodashi1=new String[num1];
biaodashi1=demo.biaodashi(num1);//接收算式
demo.setsuanshi(biaodashi1);//调用函数 给数据成员算式赋值 以便用于传递
%>
<%
for(int i=0;i<num1;i++)
{
%>
<tr><td style="120px">
<%
out.println(biaodashi1[i]);//输出表达式
%>
</td><td style="120px">
<input style="80px;height:17px;" type="text" name="result[<%=i%>]"/> <!-- 答案输入文本框 -->
</td>
</tr>
<%
}
session.setAttribute("jieshou",demo);//用于下一个界面的接收本界面的这个类的全部内容result 所以定义的对象
%>
</table>
<button id="anniu" onclick="test()" style="100px;height:30px; margin:60px 20px 200px 10px" type="submit">提交</button>
</tr>
</div>
</body>
</html>
chutishumu.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>
<br><br><br><br><br><br><br><br><br><br>
<body style="background:url(C:/Users/Administrator/Pictures/任务图片/26.png)">
<form action="Chuti.jsp" method="post">
<table align="center" border="0" width="800" style="margin:00px 200px 00px 10px">
<tr>
<td style="font-family:华文新魏;font-size:2em;500px" align="right">你想做几道题来着? </td>
<td>
<input style="100px;height:30px;" type="text" name="username" />
</td>
</tr>
<tr>
<td style="font-family:华文新魏;font-size:2em;500px" align="right">设置时间: </td>
<td>
<input style="100px;height:30px;" type="text" name="usertime" />
</td>
</tr>
<tr><td style="150px;height:40px;"></td></tr> <!-- 加了一个自己设置的高度的空行 -->
<tr align="center">
<td colspan="2">
<input style="100px;height:30px; margin:00px 20px 00px 20px" type="submit" value="开始答题" />
</body>
</html>
Error.jsp
<%@page import="com.jaovo.msg.model.arithmetic"%>
<%@ page import="javax.swing.*" %>
<%@ 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>
<div align="center">
</head>
<h1 style="font-family:华文新魏;font-size:5em">你答错的题目有</h1>
<table border="1" >
<body bgcolor=#FFE4C4>
<%
arithmetic newdemo1=new arithmetic();
newdemo1=(arithmetic)session.getAttribute("putError");
%>
<tr><td style="220px">
<%
out.print("错题");
%>
</td><td>
<%
out.print("答案");
for(int s=0;s<newdemo1.shumuerror;s++)
{
%>
</td></tr><tr><td style="220px">
<%
out.print(newdemo1.error[s]);
%>
</td><td>
<input style="100px;height:30px;" type="text" name="username" />
<%
out.println("<br/>");//换行
%>
</td></tr>
<%
}
%>
</div>
</table>
<%
out.println("<br/>");//换行
%>
<a href="Welcome.jsp">退出</a>
</body>
</html>
Result.jsp
<%@page import="com.jaovo.msg.model.arithmetic"%>
<%@ page import="javax.swing.*" %>
<%@ 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><title>出题</title></head>
<div align="center">
<body style="background:url(C:/Users/Administrator/Pictures/任务图片/27.png)">
<h1 style="font-family:华文新魏;font-size:5em">正确答案</h1>
<table border="1" >
<form action="Error.jsp" method="get">
<%
//接收客户端传递过来的参数
arithmetic newdemo=new arithmetic();
newdemo=(arithmetic)session.getAttribute("jieshou");//用于接收CHUti界面传过来的数 (对象)
String []yoursolution=new String[newdemo.shumu];//接收传过来的文本框的答案
int sumright=0,sumerror=0,empty=0;
String []error1=new String[100];
int s=0;
for(int i=0;i<newdemo.shumu;i++)
{
request.setCharacterEncoding("UTF-8");
%>
<tr><td style="220px">
<%
out.print(newdemo.suanshi[i]);//正确的算式
yoursolution[i] = request.getParameter("result["+i+"]");//你的答案
out.println(yoursolution[i]);
%>
</td><td style="300px">
<%
out.println("正确答案是: ");
out.println(newdemo.answer[i]);//正确的答案
int num1=0;
int x=1;
for(int m=0;m<yoursolution[i].length();m++)
{
num1+=(yoursolution[i].charAt(yoursolution[i].length()-m-1)-'0')*x;
x*=10;
}//字符串类型的数字转换为整型 用于和正确答案比较 因为从出题界面接受的答案是字符串类型
if(yoursolution[i].equals(""))
{
%>
</td><td >
<%
out.println("你没有回答哦!");
error1[s]=newdemo.suanshi[i];
empty++;
s++;
}
else if(num1==newdemo.answer[i])
{
%>
</td><td >
<%
sumright++;
out.println("恭喜你!回答正确!");
}
else
{
%>
</td><td >
<%
sumerror++;
error1[s]=newdemo.suanshi[i];
s++;
out.println("回答错误,再接再厉!");
}
%>
</td> </tr>
<%
}
newdemo.seterror(error1);
newdemo.shumuerror=s;
%>
<tr><td colspan="3" align="center">
<%
out.println("回答正确了"+sumright+"道题!");
out.println("<br/>");//换行
out.println("回答错误了"+sumerror+"道题!");
out.println("<br/>");//换行
out.println("没有回答"+empty+"道题!");
out.println("<br/>");//换行
%>
</td>
</tr>
</div>
</table>
<% out.println("<br/>");//换行
session.setAttribute("putError",newdemo);
%>
<button id="anniu" onclick="test()" style="100px;height:30px; margin:60px 20px 200px 10px" type="submit">进入错题本</button>
</body>
</html>
Welome.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>
<script>
function toregister()
{
window.location.href="chutishumu.jsp";
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎你!</title>
</head>
<div align="center">
</div>
<body style="background:url(C:/Users/Administrator/Pictures/任务图片/26.png)">
<table align="center" border="0" width="500" style="margin:200px 300px 40px 250px">
<tr align="center">
<td colspan="2">
<br><br>
<input style="200px;height:50px; " type="button" value="开启答题的征程" onclick="javascript:toregister();"/>
</input>
</body>
</html>
运行结果截图:
程序总结分析:
可能有些乱码,是由于UTF-8没有改过来,程序还需进一步完善,还要继续努力。
PSP表: