这次作业一直在做,一直在拖着,总感觉心里亏欠点什么,但是人就是这样,一件事情本来做起来就没有太多成功的几率,就想着延长工期,那就一直拖着吧,拖得时间越久你就会越发清晰地意识到自己已经非常触头去做那件事情了,仿佛它成了你的软肋,仿佛你命中注定就该不会去解决它一般。幸好,我还有继续做下去的勇气。。。
———来自星星的感悟
实现思路:其实思路并不难。
1. 先编写一个运行界面HTML文件,用DW做非常简单。界面中包含定量的一些数据:比如参加运算的个数,运算数据的范围
2.以及一些用户输入的数据,如:计算结果
3.和运行界面返回的数据,如:生成的计算式,以及正确的计算结果,以及用户正确回答和错误回答的个数;
4.编写运行程序.jsp文件,参数从先编写好的HTML文件中传到.jsp文件中
5.具体实现方法以及详细步骤前面的习题中已经写过,前面使用的c++语言,这儿用的Java但是思想是一致的。
以下是实现代码:
index.html文件
<!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> <style type="text/css"> <!-- a {font-family: Georgia, Times New Roman, Times, serif;font-weight: bold; } .style1 {font-family: Verdana, Arial, Helvetica, sans-serif} a:link {color: #0000FF; } a:visited {color: #FF00FF; } a:hover {color: #00FFFF; } a:active {color: #00FF55; } --> </style></head> <form action="Untitled-1.jsp" method="get"> </form><body background="1449af6eddc451daf9216940b6fd5266d21632e5.jpg"> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="771" height="49" title="落花雨"><param name="movie" value="蒋蒋%20-%20落花雨%20%5Bmqms2%5D.mp3"><param name="quality" value="high"><embed src="蒋蒋%20-%20落花雨%20%5Bmqms2%5D.mp3" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="771" height="49"></embed> </object> <div align="center"><table width="40%" border="2" cellpadding="1"><caption align="left"><div align="center" class="style1"><h2 align="center"><strong>四则运算计算器</strong></h2></div></caption><tr><th width="67%" bordercolor="#D41F00" bgcolor="#C0C0C0" scope="row">参加计算的数的个数:</th><td width="33%" align="center" valign="top" bordercolor="#0000FF" background="../1449af6eddc451daf9216940b6fd5266d21632e5.jpg" bgcolor="#FFFFFF"><div align="center" class="style1"> <select name="number" size="1"> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option> </select> <br/></div></td></tr><tr><th bordercolor="#D41F00" bgcolor="#C0C0C0" scope="row">参加计算的数据范围:</th><td bordercolor="#0000FF" background="../1449af6eddc451daf9216940b6fd5266d21632e5.jpg" bgcolor="#2A3F55"><div align="center"><input type="text" name=fanwei/><br/> </div></td></tr><tr><th bordercolor="#D41F00" bgcolor="#C0C0C0" scope="row">生成结果:</th><td bordercolor="#0000FF" background="../1449af6eddc451daf9216940b6fd5266d21632e5.jpg" bgcolor="#808080"><div align="center"> <%request.setCharacterEncoding("utf-8");String shizi= request.getParameter("Suanshi");%><%="shizi"%><br/></div></td></tr><tr><th bordercolor="#D41F00" bgcolor="#C0C0C0" scope="row">您的计算结果:</th><td bordercolor="#0000FF" background="../1449af6eddc451daf9216940b6fd5266d21632e5.jpg" bgcolor="#00FF00"><div align="center"><input type="text" name=jieguo/><br/></div></td></tr><tr><th bordercolor="#D41F00" bgcolor="#C0C0C0" scope="row">正确答案:</th><td bordercolor="#0000FF" background="../1449af6eddc451daf9216940b6fd5266d21632e5.jpg" bgcolor="#FF00FF"><div align="center"><br/></div></td></tr><tr><th bordercolor="#D41F00" bgcolor="#C0C0C0" scope="row">总计:</th><td bordercolor="#0000FF" background="../1449af6eddc451daf9216940b6fd5266d21632e5.jpg" bgcolor="#A0A0A4"><div align="center"><input type="text" name=sum/><br/></div></td></tr></table><h2 ><input type ="submit" name="submit" value="提交"> <input type ="reset" name="rein" value="重置"></h2><h2 > </h2> </div></body> </html>
.jsp文件
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="GB18030"%> <!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> <% request.setCharacterEncoding("utf-8");String n = request.getParameter("number");int number=Integer.valueOf(n);String ra = request.getParameter("rightAs");double rightAs=Double.valueOf(ra);String fw = request.getParameter("fanwei"); int fanwei=Integer.valueOf(fw);String jg = request.getParameter("jieguo"); double jieguo=Double.valueOf(jg); %> <% class Did {double Date[]=new double[10];//存放数据的数组char Operator[]=new char [11];//存放运算符的数组\char OperatorGo()//生成运算符的方法 {double a;a = Math.random() % 4;if (a == 0){return'+';}if (a == 1){return'-';}if (a == 2){return'*';}else{return'/';}}char Brackets() {return'('; } void Cun(int number, int scope)//将生成的数据存到数组内,scope为数据范围 {int control=0;for (int i = 0; i < number; i++){Date[i] =Math.random()%scope;}for (int j = 0; j < number +1; j++){double m =Math.random()*6;if ((int)m==0&&control==0){Operator[j]=Brackets();control++;}else{Operator[j] = OperatorGo();}} } void ChangeDate(int scope) {for (int i = 0; i < 10; i++){if (Date[i] == 0||Date[i]<1){Date[i] = Math.random()%scope ;}} } void ChangeOpreator(int number) {for (int i = 0; i < number; i++) {if (Operator[i] == '('&&i<=number-2){Operator[i+2] = ')';}if (Operator[i] == '('&&i >= number - 2){Operator[i] = OperatorGo();}} } String Output(int number, int scope)//输出计算式 {String Suanshi="";ChangeOpreator( number);ChangeDate(scope);for (int i = 0; i <number-1; i++){Suanshi+=Date[i];if (Operator[i] == '('){if (Operator[i - 1] == '+'){Suanshi=Suanshi+"+";}if (Operator[i - 1] == '-'){Suanshi=Suanshi+"-";}if (Operator[i - 1] == '*'){Suanshi=Suanshi+"×";}if (Operator[i - 1] == '/'){Suanshi=Suanshi+"÷";}System.out.println ( Operator[i]);}else if (Operator[i] == ')'){System.out.println ( Operator[i]);if (Operator[i - 1] == '+'){Suanshi=Suanshi+"+";}if (Operator[i - 1] == '-'){Suanshi=Suanshi+"-";}if (Operator[i - 1] == '×'){Suanshi=Suanshi+"+";}if (Operator[i - 1] == '/'){Suanshi=Suanshi+"÷";}}else{Suanshi=Suanshi+ String.valueOf(Operator[i]);}ChangeOpreator(number);ChangeDate(scope);}return Suanshi; } //以上为生成算式部分 //下面是计算方法 double Calculate(int number) {for (int k = 0; k < number - 1;k++){if (Operator[k] == '('){if (Operator[k + 1] == '+'){Date[k + 2] = Date[k + 1] + Date[k + 2];Date[k + 1] = 0;Operator[k] = '+';Operator[k + 2] = '+';}if (Operator[k + 1] == '-'){Date[k + 2] = Date[k + 1] - Date[k + 2];Date[k + 1] = 0;Operator[k] = '+';Operator[k + 2] = '+';}if (Operator[k + 1] == '*'){Date[k + 2] = Date[k + 1]*Date[k + 2];Date[k + 1] = 0;Operator[k] = '+';if (Operator[k - 1] == '+'){Operator[k+1] = '+';}if (Operator[k - 1] == '-'){Operator[k+1] = '-';}if (Operator[k - 1] == '*' ){Date[k + 1] = 1;}if (Operator[k - 1] == '/'){Date[k + 1] = 1;Operator[k + 1] = '/';}Operator[k + 2] = '+';}if (Operator[k + 1] == '/'){Date[k + 2] = Date[k + 1] / Date[k + 2];Date[k + 1] = 0;Operator[k] = '+';if (Operator[k - 1] == '+'){Operator[k + 1] = '+';}if (Operator[k - 1] == '-'){Operator[k + 1] = '-';}if (Operator[k - 1] == '*'){Date[k + 1] = 1;}if (Operator[k - 1] == '/'){Date[k + 1] = 1;Operator[k + 1] = '/';}Operator[k + 2] = '+';}}}for (int i = 0; i < number - 1; i++){if (Operator[i] == '*'){Date[i + 1] = Date[i] * Date[i + 1];Date[i] = 0;if (i == 0){Operator[i] = '+';}if (Operator[i - 1] == '+'){Operator[i] = '+';}if (Operator[i - 1] =='-'){Operator[i] = '-';}}if (Operator[i] == '/'){Date[i + 1] = Date[i] / Date[i + 1];Date[i] = 0;if (i == 0){Operator[i] = '+';}if (Operator[i - 1] == '+'){Operator[i] = '+';}if (Operator[i - 1] == '-'){Operator[i] = '-';}}}for (int j = 0; j < number - 1; j++){if (Operator[j] == '+'){Date[j + 1] = Date[j] + Date[j + 1];Date[j] = 0;}if (Operator[j] == '-'){Date[j + 1] = Date[j] - Date[j + 1];Date[j] = 0;}}return Date[number - 1];}} %><% class Do{public void main(String args[]){Did a=new Did();String Suansi;double Rightas;int R = 0, E = 0;for (int k = 0; k <number; k++){a.Cun(number, fanwei);Suansi=a.Output(number, fanwei);Rightas=a.Calculate(number);}}}%><jsp:forward page="zuoye.htm"></jsp:forward> </body> </html>
文件运行结果截图: