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]);
%>
<%
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(""))
{
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分鍾 |
改錯,補充,寫總結上課 |
課間 |