标签:text nbsp 分享 har 提高效率 范围 就是 count 个数
1.设计思想
程序中建立5个jsp文件,分别为chuandi.jsp,error.jsp,huoqu.jsp,panduan.jsp,zhengshu.jsp。
在chuandi.jsp中设置三个变量,分别题数,范围和时间。在error.jsp中输入出题个数和出题范围,在huoqu.jsp中定义一个MYSQL链接对象,连接数据库,定义String类型变量shuju,daan,在里面获取正确题目和错误题目,在panduan.jsp中输入出题数目,出题范围,答题时间,在zhengshu.jsp中输入答案。
2.源代码
chuandi.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>Insert title here</title> <style type="text/css"> body { background-image: url(huoqu1.jpg); } </style> </head> <body> <% try { int tishu=Integer.parseInt(request.getParameter("geshu")); int fanwei=Integer.parseInt(request.getParameter("fanwei")); int shijian=Integer.parseInt(request.getParameter("shijian")); session.setAttribute("tishu", tishu); session.setAttribute("fanwei", fanwei); session.setAttribute("shijian", shijian); response.setHeader("refresh","0;url = zhengshu.jsp"); } catch(Exception e) { %> <script type="text/javascript"> alert("输入错误,点击确定返回重新输入!!!") </script> <% response.setHeader("refresh","0;url = error.jsp"); } %> </body> </html>
error.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>Insert title here</title> <style type="text/css"> body { background-image: url(error.jpg); } </style> </head> <body> <form id="form2" name="form2" method="post" action="chaundi.jsp"> <div style="text-align:center;color:#F09"> <br> <br> <br> <br> <br> <br> 请输入出题的个数:<input name="geshu" type="text" value=""/><br /><br/> 请选择出题的范围:<input name="fanwei" type="text" value=""/><br/><br/> <input name="" type="submit" value="提交" /> </div> </form> </body> </html>
huoqu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.sql.Connection" import="java.sql.DriverManager" import="java.sql.PreparedStatement" import ="java.sql.ResultSet" import="java.util.ArrayList" import="java.util.List" import="java.sql.SQLException"%> <!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> <style type="text/css"> body { background-image: url(image/18.jpg); } </style> </head> <body> <%! int tishu1; String error="",zhengque=""; %> <% try { Connection con = null; //定义一个MYSQL链接对象 Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL驱动 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); //链接本地MYSQL java.sql.Statement stmt; //创建声明 stmt = con.createStatement(); String shuju=String.valueOf(session.getAttribute("shuju")); String daan=String.valueOf(session.getAttribute("daan")); // String shuju=(String)session.getAttribute("shuju"); // String daan=(String)session.getAttribute("daan"); int m=Integer.parseInt(String.valueOf(session.getAttribute("m"))); tishu1=Integer.parseInt((String.valueOf(session.getAttribute("tishu")))); String daan1=request.getParameter("daan1"); String sql="INSERT INTO grade (shizi,result,userresult) VALUES (‘"+shuju+" = "+"‘,‘"+daan+"‘,‘"+daan1+"‘)"; stmt.executeUpdate(sql); if(daan.equals(daan1)) { zhengque=zhengque+String.valueOf(m+1)+","; } else { error=error+String.valueOf(m+1)+","; } if(m+1==tishu1) { sql="INSERT INTO Grade (zhengque,cuowu) VALUES (‘"+zhengque+"‘,‘"+error+"‘)"; stmt.executeUpdate(sql); %> <form id="form2" name="form2" method="post" action="xuanze.html"> <div style="text-align:center;color:#F00"> <strong> <br> <% class User{ private String result; private String userresult; private String shizi; public String getShizi() { return shizi; } public void setShizi(String shizi) { this.shizi = shizi; } public String getResult() { return result; } public void setResult(String result) { this.result = result; } public String getUserresult() { return userresult; } public void setUserresult(String userresult) { this.userresult = userresult; } } //准备sql语句 sql = "select * from grade "; //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; //集合中只能放入user对象 List<User> users = new ArrayList<User>(); User user = null; try { preparedStatement = con.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while(resultSet.next()) { user = new User(); user.setShizi(resultSet.getString("Shizi")); user.setResult(resultSet.getString("result")); user.setUserresult(resultSet.getString("userresult")); users.add(user); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } /*finally { DBUtil.close(resultSet); DBUtil.close(preparedStatement); DBUtil.close(con); }*/ for( User user1 : users ){ %> <br> <tr> <td> <%=user1.getShizi() %></td> <td> <%=user1.getResult() %></td> <td> <%=user1.getUserresult() %></td> <br> <% } %> <% out.print("<br>"+"正确的题目是:"+zhengque+"<br>"+"错误的题目是:"+error+"<br>"+"点击确定返回主界面"); zhengque=""; error=""; %> <input name="" type="submit" value="确定" /> </strong> </div> </form> <% } else { response.setHeader("refresh","0;url = zhengshu.jsp"); } } catch (Exception e) { System.out.print("MYSQL ERROR:" + e.getMessage()); } %> </body> </html>
panduan.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>Insert title here</title> <style type="text/css"> body { background-image: url(panduan.jpg); } </style> </head> <body> <form id="form2" name="form2" method="post" action="chuandi.jsp"> <div style="text-align:center;color:#F09"> <br> <br> <br> <br> <br> <br> 请输入出题的个数:<input name="geshu" type="text" value=""/><br /><br/> 请选择出题的范围:<input name="fanwei" type="text" value=""/><br/><br/> 请选择答题时间:<input name="shijian" type="text" value=""/><br/><br/> <input name="" type="submit" value="提交" /> </div> </form> </body> </html>
zhengshu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.Random" import="java.sql.Connection" import="java.sql.DriverManager"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <% String url = basePath; //设置跳转的地址 %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style type="text/css"> body { background-image: url(zhengshu.jpg); } </style> </head> <body> <h2 style=color:red><span id=jump>10</span> 秒钟答题结束</h2> <%! class Suan1//生成整数计算的类 { private int x; private int y; private int operate;//定义两个变量,还有一个符号变量 public Random random=new Random();//定义随机数 private String jisuanshi;//生成的计算式储存在jisuanshi里面 int fanwei; public String shengcheng(int fanwei1)//自动生成数的函数 { fanwei=fanwei1; operate=random.nextInt(4);//生成符号 String fuhao; x=random.nextInt(fanwei);//生成100以内的自然数 y=random.nextInt(fanwei); //x=(int)Math.floor(Math.random()*(fanwei+1)+1); // y=(int)Math.floor(Math.random()*(fanwei+1)+1); if(operate==0)//0是加法 { fuhao="+"; jisuanshi=String.valueOf(x)+fuhao+String.valueOf(y); } else if(operate==1)//1是减法 { fuhao="-"; if(x>=y) { jisuanshi=String.valueOf(x)+fuhao+String.valueOf(y); } else { jisuanshi=String.valueOf(y)+fuhao+String.valueOf(x); } } else if(operate==2)//乘法 { fuhao="*"; x=random.nextInt(10);//生成10以内的自然数 y=random.nextInt(10); jisuanshi=String.valueOf(x)+fuhao+String.valueOf(y); } else { fuhao="/";//除法 x=random.nextInt(10); y=random.nextInt(10); int z=x*y; for(int i=0;;i++) { if(y==0) { x=random.nextInt(10); y=random.nextInt(10); z=x*y; } else { break; } } x=z; jisuanshi=String.valueOf(x)+fuhao+String.valueOf(y); } return jisuanshi; } public String shuchu()//定义输出函数 { if(operate==0) { return (String.valueOf(x+y));//并且返回计算的结果 } else if(operate==1) { if(x>=y)//将较大的数放在前面,不能出现负数 { return (String.valueOf(x-y)); } else { return (String.valueOf(y-x)); } } else if(operate==2) { return (String.valueOf(x*y));//存在问题 888888888888 } else//存在问题 99999999999999999 { //if(x%y==0) //{ return (String.valueOf(x/y)); //} //else //{ //if(x>y) //{ // int shang=x/y; // int yushu=x-y*shang; //return (String.valueOf(shang)+"‘"+String.valueOf(yushu)+"/"+String.valueOf(y)); //} //else //{ //return (String.valueOf(x)+"/"+String.valueOf(y)); //} // } } } public int fanhuioperate() { return operate; } public int jianyan(String a[],int s)//检验是否有重复的式子,如果有返回0,没有就返回1 { int flag=1; for(int i=0;i<s;i++) { if(jisuanshi.equals(a[i]))//数组a里面是生成的式子 { flag=0; break; } } return flag; } } int tishu; int fanwei; int m=0; String shuju[]=new String[10000]; int flag=0; Suan1 a=new Suan1(); String daan,daan1; String zhengque="",cuowu=""; %> <% tishu=Integer.parseInt((String.valueOf(session.getAttribute("tishu")))); fanwei=Integer.parseInt((String.valueOf(session.getAttribute("fanwei")))); session.setAttribute("tishu", tishu); if(m<tishu)//生成30个计算式 { shuju[m]=a.shengcheng(fanwei);//将生成的计算式储存在数组里面 flag=a.jianyan(shuju, m);//返回是否有重复 if(flag==1)//如果没有就是1 { if(m+1==tishu) {daan=a.shuchu(); session.setAttribute("shuju", shuju[m]); session.setAttribute("daan", daan); session.setAttribute("m", m); %> <form id="form2" name="form2" method="post" action="huoqu.jsp"> <div style="text-align:center"> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <%out.print(shuju[m]+"="); %> <br> <br> <br> <br> 请输入答案:<input name="daan1" type="text" value=""/><br /><br/> <input name="" type="submit" value="提交" /> </div> </form> <% } else { daan=a.shuchu(); session.setAttribute("shuju", shuju[m]); session.setAttribute("daan", daan); session.setAttribute("m", m); %> <form id="form2" name="form2" method="post" action="huoqu.jsp"> <div style="text-align:center"> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <%out.print(shuju[m]+"=");%> <br> <br> <br> <br> 请输入答案:<input name="daan1" type="text" value=""/><br /><br/> <input name="" type="submit" value="下一个" /> </div> </form> <% m++; flag=0; } } else { response.setHeader("refresh","1;url = zhengshu.jsp"); } } %> </body> <script> function countDown(secs){ jump.innerText=secs; if(--secs>0) setTimeout("countDown("+secs+" )",1000); } countDown(10); function jumpToIndex(){ window.location.href=‘<%=url %>‘; } </script>
3.实验截图
4.编程总结分析
我们小组做这道题时,开始无从下手,对网页版了解的并不是很多。通过查阅课本或者在网上查找资料,有一定的了解。在编写这道题时,存在很多处的错误。有的代码我们也就借助周六给我们讲课的老师的代码,在一定的基础上加以改正。通过这次编程我们也明白了合作的重要性,有时一个人并不能处处兼顾,相反和别人合作还可以提高效率。
标签:text nbsp 分享 har 提高效率 范围 就是 count 个数
原文地址:http://www.cnblogs.com/lijing925/p/7994491.html