四则运算————javaweb版

 

1.设计思路:

定义一个类arithmetic,在该类中的定义相关成员,随机产生的题目以及答案用数组承接,在第一个jsp里面用户输入题目数量以及设置做题时间,将这两个数传到第二个jsp页面,在此页面定义类对象,调用相关类函数,进行出题:

最后将算式的数组和答案的数组以及用户输入的值传到第三个jsp页面,进行答案的校对即可。

源代码:

arithmetic.java:

 

 

package com.jaovo.msg.model;

public class arithmetic {
public int []answer;//答案
public int shumu;//出题数目
public String []suanshi;//算式
public void setsuanshi(String []suanshi)
{
this.suanshi=suanshi;
}

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;
}

}

 

 chutijiemian.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>
<h1 style="font-family:华文新魏;font-size:5em">WELCOME</h1>
<body style="background:url(C:/Users/888888/Desktop/8.jpg)">

<form action="Chuti.jsp" method="post">
<table align="center" border="0" width="500" style="margin:00px 200px 00px 5px">
<tr>
<td style="font-family:华文新魏;font-size:2em;width:500px" align="right">你想做几道题来着? </td>
<td>
<input style="width:100px;height:30px;" type="text" name="username" />
</td>
</tr>

<tr>
<td style="font-family:华文新魏;font-size:2em;width:500px" align="right">设置时间: </td>
<td>
<input style="width:100px;height:30px;" type="text" name="usertime" />
</td>
</tr>

<tr><td style="width:150px;height:40px;"></td></tr> <!-- 加了一个自己设置的高度的空行 -->
<tr align="center">
<td colspan="2">
<input style="width:100px;height:30px; margin:00px 20px 00px 150px" type="submit" value="开始答题" />
</body>
</html>

 

 

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 bgcolor=#FFE4C4 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>
<h1 style="font-family:华文新魏;font-size:4em" >开始答题</h1>
<td style="font-family:华文新魏;font-size:1em;width:500px" align="right">倒计时:</td>
<p id = "txt"></p>
<form action="Result.jsp" onsubmit="return validate()==1" 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++)
{
out.println(biaodashi1[i]);//输出表达式
%>
<input style="width:80px;height:17px;align="right"" type="text" name="result[<%=i%>]"/> <!-- 答案输入文本框 -->
<%
out.println("<br/>");
out.println("<br/>");//换行
}
session.setAttribute("jieshou",demo);//用于下一个界面的接收本界面的这个类的全部内容result 所以定义的对象
%>
<tr>
<button id="anniu" onclick="test()" type="submit">提交</button>

</tr>
</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>

<body bgcolor=#FFE4C4>
<h1 style="font-family:华文新魏;font-size:5em">正确答案</h1>
<%
//接收客户端传递过来的参数
arithmetic newdemo=new arithmetic();
newdemo=(arithmetic)session.getAttribute("jieshou");//用于接收CHUti界面传过来的数 (对象)
String []yoursolution=new String[newdemo.shumu];//接收传过来的文本框的答案
int sumright=0,sumerror=0,empty=0;
for(int i=0;i<newdemo.shumu;i++)
{
request.setCharacterEncoding("UTF-8");
out.print(newdemo.suanshi[i]);//正确的算式
yoursolution[i] = request.getParameter("result["+i+"]");//你的答案
out.println(yoursolution[i]);
%>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<%
out.println("正确答案是: ");
out.println(newdemo.answer[i]);//正确的答案
%>
&nbsp;&nbsp;&nbsp;&nbsp;
<%
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(""))
{
out.println("你没有回答哦!");
empty++;
}
else if(num1==newdemo.answer[i])
{
sumright++;
out.println("恭喜你!回答正确!");
}
else
{
sumerror++;
out.println("回答错误,再接再厉!");
}
out.println("<br/>");//换行
}
out.println("回答正确了"+sumright+"道题!");
out.println("<br/>");//换行
out.println("回答错误了"+sumerror+"道题!");
out.println("<br/>");//换行
out.println("没有回答"+empty+"道题!");
out.println("<br/>");//换行
%>
</tr>
<a href="chutishumu.jsp">退出</a>
</body>
</html>

运行结果:

 

 

 

 

已接触Javawe快一个月了,因为是刚刚接触这个网页吧,所以会的很少,不会的很多。对于Javaweb,我感觉是个很神奇的,参数的传递,自动调用,自动启动等等,这些都是没见过的,当然还要进一步理解!写的慢的原因,对Javaweb不是很了解,只能一步一步来

 

 

时间记录日志:

                                                                               学生:马佳慧                                            日期:2017/12/5

                                                                               教师:王建民                                            课程:软件工程概论

日期\时间

开始时间

结束时间

中断时间

净时间

活动

备注

12/1

9:00

11:30

10:30

120分钟

自习,练习

课间

 

14:00

16:30

15:30

80分钟

练习

中场休息

12/2

9:30

11:00

10:30

80分钟

听课,练习

中场休息

 

14:30

18:00

16:00

200分钟

上课

中场休息

12/3

8:00

11:30

10:00

150分钟

写作业,练习,提交作业

写 四则

12/4

19:00

22:00

20:30

150分钟

练习,自习

课间

 12/5

8:00

16:50

8:50/9:50/12:00

180分钟

改错,补充,写总结上课

课间

posted @ 2017-12-05 15:47  小程大序的猿  阅读(2538)  评论(0编辑  收藏  举报