软工概论第二周个人项目四则运算二(改进)
设计思想:
设计思想:输出表达式的个数n,利用随机数方法产生小于100的随机数,用for循环产生多个随机数,然后在进行判断,判断两个证书的和小于100,差大于0,乘积小于100,能够整除如果不符合要求则跳过本次循环,如果符合则赋值当总数到n时跳出循环,用字符串保存表达式,用double类型的数组保存正确结果,然后循环输出表达式,输入结果后跳转到判断结果界面,循环判断结果,如果相等flag[]为1,不等为0,最后给出正确的题目数量和错误的数量和未作的数量,在循环输出不正确的那些题目
改进的地方:表达式数量输入为空或者为0的优化,结果输入时意外添加空格的优化,最后输出不正确的题目,在线答题时时间限制,更加人性化
//Suanshijava package p1; public class Suanshi { final int MAX=1000; double[] e= new double[MAX]; String[] f=new String[MAX]; private int num; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public Suanshi(){} public Suanshi(int n) { creatSuanshi(n); this.num=n; } public void creatSuanshi(int n) { int a; int c; int d; for(int i=0;i<n;i++) { a=(int)(Math.random()*100+1); d=(int)(Math.random()*4+1); c=(int)(Math.random()*100+1); switch(d){ case 1: if(a+c>100) { i--; break; } e[i]=a+c; f[i]=""+a+"+"+c; break; case 2: if(a-c<0) { i--; break; } e[i]=a-c; f[i]=""+a+"-"+c; break; case 3: while(a*c>100) { a=(int)(Math.random()*100+1); c=(int)(Math.random()*100+1); } e[i]=a*c; f[i]=""+a+"*"+c; break; case 4: e[i]= (int)(Math.random()*10+1); c= (int)(Math.random()*10+1); a=(int)(c*e[i]); f[i]=""+a+"/"+c; if((c==0)&&(a>100)) { i--; break; } break; } } } public String getSuanshi(int i) { return f[i]; } public double getDaan(int i) { return e[i]; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>输入数量页面</title> </head> <body background="C:\Users\DELL\Desktop\photo1.jpg"> <br><br><br><br><br><br><br> <% String loginError = (String)request.getAttribute("loginError"); if(loginError == null) { loginError = ""; } %> <form action="add.jsp" method="get"> <center><h1>输入页面</h1> 数量:<input type="text" name="number"/><%=loginError %><br><br> 时间一共为<font color="red">100</font>秒<br><br> <input type="submit" value="开始答题" /></center> </form> </body> </html>
<%@page import="p1.Suanshi"%> <%@ 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> <body onLoad="TimeClose();"> <script language="javascript"> var cTime=100; function TimeClose() { window.setTimeout('TimeClose()',1000); if(cTime<=0) CloseWindow_Click(); this.ShowTime.innerHTML="倒计时"+cTime+"秒后结束答题"; cTime--; } function CloseWindow_Click() { window.close(); } </script> <% //获取客户端传递过来的参数 if("".equals(request.getParameter("number").trim())) { request.setAttribute("loginError","不能为空"); %> <jsp:forward page="index.jsp"></jsp:forward> <% } else { if(Integer.parseInt(request.getParameter("number").trim())==0) { request.setAttribute("loginError","不能为0"); %> <jsp:forward page="index.jsp"></jsp:forward> <% } } int number=Integer.parseInt(request.getParameter("number").trim()); Suanshi suanshi=new Suanshi(number); session.setAttribute("suanshi", suanshi); %> <center><h1>答题</h1></center> <div align="right" id="ShowTime">倒计时100秒后结束答题</div> <hr> <form action="judge.jsp"> <table align="center" border="1" width="450"> <tr> <td align="center">题号</td> <td align="center">表达式</td> <td align="center">你的答案</td> </tr> <% for(int i=0;i<number;i++) { %> <tr> <td align="center">第<%=i+1%>题</td> <td align="center"><%=suanshi.getSuanshi(i)%>=</td> <td><input type="text" name="daan<%=i%>"></td> </tr> <% } %> </table><br> <center><input type="submit" value="提交"></center> </form> </body> </html>
<%@page import="p1.Suanshi"%> <%@ 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>Insert title here</title> </head> <% Suanshi suanshi = (Suanshi)session.getAttribute("suanshi") ; int dui=0; int cuo=0; int meizuo=0; int[] flag=new int[suanshi.getNum()]; String daan[]=new String[suanshi.getNum()]; double daan1[]=new double[suanshi.getNum()]; for(int i=0;i<suanshi.getNum();i++) { daan[i]=request.getParameter("daan"+i).trim(); if((daan[i]==null)||("".equals(daan[i].trim()))) { flag[i]=0; meizuo++; } else { daan1[i] =Double.parseDouble(daan[i]); if(Math.abs(daan1[i]-suanshi.getDaan(i))<0.01) { flag[i]=1; dui++; } else { flag[i]=0; cuo++; } } } %> <body> <hr size="1" color="black"> <center> <h1>测试成绩</h1> <hr size="1" color="black"> 一共:<%=suanshi.getNum()%>道<br> 没做的:<%=meizuo %>道<br> 做错的:<%=cuo %>道<br> 作对的:<%=dui %>道<br></center> <hr width="800" color="red"> <center><h3>错误题目</h3></center> <hr width="800" color="red"> <table align="center" border="1" width="450"> <tr> <td align="center">题号</td> <td align="center">表达式</td> <td align="center">正确答案</td> <td align="center">你的结果</td> </tr> <% for(int i=0;i<suanshi.getNum();i++) if(flag[i]==0) { %> <tr> <td align="center">第<%=i+1%>题</td> <td align="center"><%=suanshi.getSuanshi(i)%>=</td> <td align="center"><%=suanshi.getDaan(i)%></td> <td align="center"><%=daan[i]%></td> </tr> <% } %> </table> </body> </html>