要求:把程序变成一个网页程序或智能手机程序, 用户通过设定参数,就可以得到各种题目,并可实现在线答题并评判。
代码:
首页:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>welcom</title> 6 </head> 7 <body> 8 9 <body background="images/2.jpg"> 10 <div class="STYLE1" style=" 100%; font-family: 楷体; color:red;text-align: center"> 11 <h1> 12 <marquee direction="right" scrollamount="4" >欢迎使用四则运算出题器</marquee> 13 </h1> 14 15 </div> 16 <td align="center"> 17 <form action="MyHtml.html" method="post"> 18 <center> 19 <div style=" padding:250px"> 20 <input type="submit" name="submit" style="text-align:center,vertical-align:middle;400px;height:70px;background: blue;font-size:20px" value="我已准备好,请开始出题" > 21 <input type="submit" name="submit" style="text-align:center,vertical-align:middle;400px;height:70px;background: blue;font-size:20px" value="我没准备好,退出系统" onClick="window.close()";> 22 </div></form> 23 </td> 24 </center> 25 </body> 26 </html>
选择功能界面:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>功能选择</title> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 6 </head> 7 <body background="images/6.jpg"> 8 <div class="STYLE1" style=" 100%; font-family: 楷体; color:red; text-align: center"> 9 <h1> 10 <marquee direction="right" scrollamount="4" >欢迎使用四则运算出题器</marquee> 11 </h1> 12 </div> 13 <form action="index.jsp" method="post"> 14 <div style=" padding:100px"> 15 <center> 16 <br>请输入最大值: 17 <input name="MaxNumber" type="text"><br> 18 <br>请输入最小值: 19 <input name="MinNumber" type="text"><br> 20 <br>请选择是否有乘除: 21 <input type="checkbox" name="function" value=1>有 <br> 22 <br>请选择是否有括号: 23 <input type="checkbox" name="function" value=3>有 <br> <br> 24 </div> 25 </center> 26 <center> 27 <input type="submit" style="text-align:center,vertical-align:100px;height:30px;color: blue" name="submit" value="确定"> 28 <input type="button" style="text-align:center,vertical-align:100px;height:30px;color: blue" value="返回" onClick="window.location.href='Welcom.html'"> 29 </center> 30 </form> 31 </body> 32 </html>
答题界面:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 3 pageEncoding="utf-8"%> 4 <%@ page import="test5.*"%> 5 <% 6 String path = request.getContextPath(); 7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 13 <title>答题</title> 14 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 15 </head> 16 <body background="images/11.gif"> 17 <div class="STYLE1" style=" 100%; font-family: 楷体; color:red; text-align: center"> 18 <h1> 19 <marquee direction="right" scrollamount="4" >欢迎使用四则运算出题器</marquee> 20 </h1> 21 </div> 22 <% 23 String[] s=new String[30]; 24 float k[]=new float[30]; 25 Calculate a=new Calculate(); 26 request.setCharacterEncoding("UTF-8"); 27 String[] function = request.getParameterValues("function"); 28 String MaxNumber,MinNumber; 29 MaxNumber=request.getParameter("MaxNumber"); 30 MinNumber=request.getParameter("MinNumber"); 31 a.SetValue(a.StrToNum(MaxNumber),a.StrToNum(MinNumber)); 32 if(function!=null){ 33 for(int i=0;i<function.length;i++){ 34 if(function[i].equals("1")) 35 { 36 a.SetValue1(1); 37 } 38 if(function[i].equals("3")) 39 { 40 a.SetValue3(0); 41 } 42 } 43 } 44 %> 45 <center> 46 <form action="MyJsp.jsp" method="post"> 47 <% 48 a.main(); 49 float temp; 50 String[] se=new String[30]; 51 for(int i=0;i<30;i++){ 52 s[i]=a.cs(i); 53 se[i]="name"; 54 se[i]=se[i]+i; 55 k[i]=a.css(i); 56 out.print(s[i]); 57 %> 58 <input type="text" style="color: blue;50px;height:21px;" name=<%=se[i] %>> 59 <% out.print("      "); %> 60 <% 61 if((i+1)%3==0) 62 out.print("<br><br>");%> 63 <% 64 } 65 session.setAttribute( "v1",s); 66 session.setAttribute( "v2",k); 67 %> 68 </center> 69 70 71 <center> 72 <br><input type="submit" style="80px;height:60px;color: blue" name="submit" value="答题完成"> 73 <input type="button" style="80px;height:60px;color: blue" value="返回首页" onClick="window.location.href='Welcom.html'"> 74 </center> 75 </form> 76 </body> 77 </html>
判断答对几道题界面:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" import="java.util.*" pageEncoding="gb2312" contentType="text/html; charset=gb2312"%> 2 <%@ page import="test5.*"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 6 %> 7 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 9 <html> 10 <head> 11 <base href="<%=basePath%>"> 12 <title>finally</title> 13 </head> 14 <body background="images/8.jpg"> 15 <% request.setCharacterEncoding("UTF-8");%> 16 <div class="STYLE1" style=" 100%; font-family: 楷体; color:red; text-align: center"> 17 <h1> 18 <marquee direction="right" scrollamount="4" >谢谢使用四则运算出题器</marquee> 19 </h1> 20 </div> 21 <center> 22 <div style=" padding:250px; font-family: 楷体;font-size:30px; color:blue; text-align: center"> 23 <% 24 String[] name=new String[30]; 25 String[] sum=new String[30]; 26 Calculate a=new Calculate(); 27 float sum1[]=new float[30]; 28 int right=0; 29 String[] s= (String[])session.getAttribute("v1"); 30 float[] k=(float[])session.getAttribute("v2"); 31 for(int i=0;i<30;i++) 32 sum1[i]=0; 33 for(int i=0;i<30;i++){ 34 name[i]="name"; 35 name[i]=name[i]+i; 36 37 sum[i]=request.getParameter(name[i]); 38 if(((int)(k[i]*100))%100!=0) 39 sum1[i]=a.StrToFloat1(sum[i]); 40 else 41 sum1[i]=a.StrToFloat(sum[i]); 42 if(Math.abs(sum1[i]*100-k[i]*100)<4){ 43 right++; 44 } 45 } 46 out.print("总共做对 "+right+"道题!继续努力!"); 47 %><br> 48 <br><br> 49 <input type="button" style="100px;height:70px;background: blue;color: yellow" value="返回首页" onClick="window.location.href='Welcom.html'"> 50 <input type="button" style="100px;height:70px;background: blue;color: yellow" value="退出系统" onClick="window.close();"> 51 </center> 52 </div> 53 </body> 54 </html>
主程序,主要功能实现代码:
计算两个数结果:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 static float Operate(float first, char oper1, float second) 2 { 3 switch (oper1) 4 { 5 case '+': 6 { 7 return (first + second); 8 } 9 case '-': 10 { 11 if (first-second<0) 12 { 13 IsRep = 1; 14 return 0; 15 } 16 return (first - second); 17 } 18 case '*': 19 { 20 return (first * second); 21 } 22 case '/': 23 { 24 if (second == 0) 25 { 26 IsRep = 1; 27 return 0; 28 } 29 return (first / second); 30 } 31 default: return 0; 32 } 33 }
加括号:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 static void AddLbracket(){ 2 for (int j = 0; j < 2; j++) 3 LBraket[j] = 0; 4 if (Amount == 2) 5 { 6 BracketNum = 0; 7 } 8 if (Amount == 3){ 9 BracketNum = (int)((Math.random())*2); 10 } 11 if (Amount > 3) 12 { 13 BracketNum = (int)((Math.random())*3); 14 } 15 for (int i = 0; i < BracketNum; i++){ 16 LBraket[i] = 1 + (int)((Math.random())*(Amount - 2)); 17 } 18 } 19 //加右括号 20 static void AddRbracket(){ 21 for (int j = 0; j < 2; j++) 22 RBraket[j] = 0; 23 int choose; 24 int trance; 25 if (BracketNum == 1){ 26 RBraket[0] = LBraket[0] + 1 +(int)((Math.random())* (Amount - LBraket[0])); 27 } 28 if (BracketNum == 2) 29 { 30 //把最左边的左括号放在第一个数组中 31 if (LBraket[0] < LBraket[1]) 32 { 33 trance = LBraket[0]; 34 LBraket[0] = LBraket[1]; 35 LBraket[1] = trance; 36 } 37 //当两个左括号之间相差有点远时有2中右括号添加方法 38 if (LBraket[0] - LBraket[1]>2){ 39 choose = (int)((Math.random())*2); 40 if (choose == 0){ 41 RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); 42 RBraket[1] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); 43 } 44 if (choose == 1) 45 { 46 RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); 47 RBraket[1] = LBraket[1] + 1 + (int)((Math.random())*(LBraket[0] - 2)); 48 } 49 } 50 else 51 { 52 RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); 53 RBraket[1] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0])); 54 if (LBraket[0] == LBraket[1] && RBraket[0] == RBraket[1]){ 55 LBraket[0] = LBraket[1] = 0; 56 RBraket[0] = RBraket[1] = 0; 57 BracketNum = 0; 58 59 } 60 if (LBraket[1] == 1 && (RBraket[0] == Amount || RBraket[1] == Amount)) 61 { 62 LBraket[0] = LBraket[1] = 0; 63 RBraket[0] = RBraket[1] = 0; 64 BracketNum = 0; 65 } 66 } 67 } 68 }
随机生成操作符:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 static void Operater() 2 { 3 int choose; 4 char op='+'; 5 if (IsMulDlvExist == 1) 6 choose = 1 + (int)((Math.random())* 4); 7 else 8 choose = 1 + (int)((Math.random())*2); 9 switch (choose) 10 { 11 case 1:{op = '+'; lastOp = '+'; break; } 12 case 2:{op = '-'; lastOp = '-'; break; } 13 case 3:{op = '*'; lastOp = '*'; break; } 14 case 4: 15 { 16 //防止连续除法产生运算误区 17 op = '/'; 18 if (lastOp == '/') 19 IsRep = 1; 20 else 21 lastOp = '/'; 22 break; 23 } 24 } 25 formula += op; 26 }
创建算式:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 static void CreateNumber(){ 2 for (int k = 1; k <= Amount; k++) 3 { 4 5 for (int i = 0; i < 2; i++){ 6 if (LBraket[i] == k) 7 formula += "("; 8 } 9 Integer(); 10 for (int j = 0; j < 2; j++){ 11 if ((RBraket[j] == k) && RBraket[j] != 0) 12 formula += ")"; 13 } 14 if (k == Amount) 15 formula += "="; 16 else 17 Operater(); 18 } 19 }
运行截图:
缺陷记录日志:
不能产生分数,另外不能判断是否有负数。
时间记录日志:
在求数组中最大子数组的和03这篇日志博客中