码迷,mamicode.com
首页 > 其他好文 > 详细

四则运算2的设计思想+源程序代码+运行结果截图+编程总结分析

时间:2017-12-06 22:01:44      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:gets   页面   trace   generate   ==   span   个人   pre   while   

(一)程序设计思想:

  在第一个页面中输入要出的题数,然后接收这个数据去往数据库中写入相应数量的四则运算题目。在第二个jsp页面中遍历输出数据库中的题目,并且加入一个文本框去让客户输入这个数据,当所有题都答完是下面有一个提交按钮,点击提交按钮,将用户输入的结果与数据库中结果进行比对,然后跳转到页面3去输出题目,用户结果,和判断结果,页面3有两个按钮分别是继续答题和推出,如果选择继续答题则跳转到页面1.

(二)源程序代码:

 

public class Dao {
public void add(Connection conn ,users u) throws SQLException
{
try{
Statement stmt = conn.createStatement();//浠庢暟鎹簱閲岄潰鍙栦笢瑗垮姣?
PreparedStatement sql =conn.prepareStatement("insert into yunsuan(num1,num2,symbol,find)values(?,?,?,?)");
sql.setInt(1,u.num1);
sql.setInt(2,u.num2);
sql.setString(3,u.symbol);
sql.setInt(4,u.find);
int rtn=sql.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
}
}

}

public class DBUtil {
//eshop涓烘暟鎹簱鍚嶇О锛宒b_user涓烘暟鎹簱鐢ㄦ埛鍚峝b_password涓烘暟鎹簱瀵嗙爜
public static String db_url = "jdbc:sqlserver://localhost:1433; DatabaseName=users";
public static String db_user = "sa";
public static String db_password = "20163488";

public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(db_url, db_user, db_password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

public static void close(Statement state, Connection conn) {
if(state!=null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public static void close(ResultSet rs, Statement state, Connection conn) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(state!=null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}


public class YunServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public YunServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doPost(request,response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
dao.DBUtil db=new dao.DBUtil();
Connection conn =db.getConn();
Dao open=new Dao();
int num=Integer.parseInt(request.getParameter("num"));
int time=Integer.parseInt(request.getParameter("time"));
HttpSession session = request.getSession();
session.setAttribute("time", time);
//HttpSession session=request.getSession();
//session.setAttribute("num", num);
Random r=new Random();
for(int i=0;i<num;i++)
{
users u=new users();
int r1=r.nextInt(100)+1;
int r2=r.nextInt(100)+1;
int r3=r.nextInt(4)+1;
switch(r3)
{
case 1:
{
int x=r1+r2;
while(x>100||x<0)
{
r1=r.nextInt(100)+1;
r2=r.nextInt(100)+1;
x=r1+r2;
}
u.num1=r1;
u.num2=r2;
u.symbol="+";
u.find=x;

try {
open.add(conn, u);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
case 2:
{
int x=r1-r2;
while(x>100||x<0)
{
r1=r.nextInt(100)+1;
r2=r.nextInt(100)+1;
x=r1-r2;
}
u.num1=r1;
u.num2=r2;
u.symbol="-";
u.find=x;
try {
open.add(conn, u);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
case 3:
{
int x=r1*r2;
while(x>100||x<0)
{
r1=r.nextInt(100)+1;
r2=r.nextInt(100)+1;
x=r1*r2;
}
u.num1=r1;
u.num2=r2;
u.symbol="*";
u.find=x;
try {
open.add(conn, u);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
case 4:
{
int x=r1/r2;
int root=r1%r2;
while((x>100)||(root!=0)||(x<0)||(r1<r2))
{
r1=r.nextInt(100)+1;
r2=r.nextInt(100)+1;
x=r1/r2;
root=r1%r2;
}
u.num1=r1;
u.num2=r2;
u.symbol="/";
u.find=x;
try {
open.add(conn, u);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}
request.getRequestDispatcher("../MyJsp.jsp").forward(request,response);
}


/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}

 

public class users {
public int num1;
public int num2;
public String symbol;
public int find;
public int getNum1() {
return num1;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
this.num2 = num2;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public int getFind() {
return find;
}
public void setFind(int find) {
this.find = find;
}
}

 

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>主页</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
#form2 p {
text-align: center;
}
#form1 p label {
text-align: center;
}
#form1 {
text-align: center;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body background="timg.jpg">
<form id="form2" name="form2" method="post" action="">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</form>
<form id="form1" name="form1" method="post" action="servlet/YunServlet?method=add">
<p>
<span id="form1">
<label for="name">要做的题数:</label>
<input type="text" name="num" id="num" />
</span></p>
<p>
<label for="name2">限制的时间:</label>
<input type="text" name="time" id="time" />
</p>

<p>
<input type="submit" name="tijiao" id="tijiao" align="center" value="提交" />
</p>
</form>
</body>
</html>

 

 

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>做题页</title>
<style type="text/css">
.aaa {
text-align: center;
font-weight: bold;
}
.aa {
text-align: center;
}
#tijiao {
text-align: center;
}
#form p label {
text-align: center;
}
#form1{
text-align: center;
}
.qqq {
text-align: center;
font-size: 36px;
font-family: "微软雅黑";
}
</style>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
<body background="timg.jpg">
<p>&nbsp; </p>
<p class="qqq">做题页</p>
<p>&nbsp;</p>
<p>
<form id="form1" name="form1" method="post" action="MyJsp1.jsp">
<table width="386" border="1" align="center">
<tr>
<td width="79" height="23" class="aaa">题号</td>
<td width="116" class="aaa">题目</td>
<td width="169" class="aaa">答案</td>
</tr>


<%
int time1=(int)session.getAttribute("time");
dao.DBUtil db=new dao.DBUtil();
Connection conn =db.getConn();
Statement stmt = conn.createStatement();//从数据库里面取东西对比
ResultSet rs=stmt.executeQuery("select * from yunsuan");
int x=0;
int y=1;
while(rs.next())
{
x++;
int num1=rs.getInt("num1");
int num2=rs.getInt("num2");
String symbol=rs.getString("symbol");
%>
<table width="386" border="1" align="center">
<tr>
<td width="79" height="23" class="aa"><%=y %>.</td>
<td width="116" class="aa"><%=num1+symbol+num2 %>=</td>
<td width="169" class="aa">
<input type="text" name="<%=x %>" id="<%=x %>" />
</td>
</tr>
</table>
<%
y++;
}
%>
</table>
<p>&nbsp;</p>
<input type="submit" name="tijiao" id="tijiao" value="提交" />
</form>
<p>&nbsp; </p>
    <head>
      <script type="text/javascript">
        var time =<%=time1%>; //时间,秒
        function Redirect() {
        window.location = "MyJsp1.jsp";
        }
        var i = 0;
        function dis() {
        document.all.s.innerHTML = "还剩" + (time - i) + "秒";
        i++;
        }
        timer = setInterval(‘dis()‘, 1000); //显示时间
        timer = setTimeout(‘Redirect()‘, time * 1000); //跳转
      </script>
  </head>
<table width="507" border="1" align="center">
    <td width="229" class="a" style="text-align: center; color: #F00;"><span id="s"></span></td>
</table>
</body>
</html>

 

 

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>答案页</title>
<style type="text/css">

#form1 p label {
text-align: center;
}
#form1 {
text-align: center;
}
</style>
<style type="text/css">
.a {
color: #0F0;
}
.qqq {
text-align: center;
font-size: 36px;
font-family: "微软雅黑";
}
</style>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body background="timg.jpg">
<p>&nbsp; </p>
<p class="qqq">结果页</p>
<p>&nbsp;</p>
<p>
<%
dao.DBUtil db=new dao.DBUtil();
Connection conn =db.getConn();
Statement stmt = conn.createStatement();//从数据库里面取东西对比
ResultSet rs=stmt.executeQuery("select * from yunsuan");
int x=0;
double q=0;//对题数
double p=0;//错题数
while(rs.next())
{
x++;
String y=String.valueOf(x);
int num1=rs.getInt("num1");
int num2=rs.getInt("num2");
String symbol=rs.getString("symbol");
int find=rs.getInt("find");
int a=0;

if(request.getParameter(y)!=null&&(!"".equals(request.getParameter(y))))
{
a=Integer.parseInt(request.getParameter(y));
}
%>
</p>
<table width="507" border="1" align="center">
<tr>
<td width="131" style="text-align: center"><%=num1+symbol+num2 %>=</td>
<td width="125" style="text-align: center"><%=a %></td>

<%
if(find==a)
{
q++;
%>
<td width="229" class="a" style="text-align: center">回答正确</td>
<%
}
else
{
p++;
%>
<td width="229" class="a" style="text-align: center; color: #F00;">回答错误,正确答案为<%=find %></td>
<%

}
}
%>
</tr>
</table>
<%
stmt.executeUpdate("delete from yunsuan");

%>
<br>
<br>
<br>
<table width="700" border="1" align="center">
<tr>
<td width="200" height="23" class="aaa">总题数:<%=q+p%></td>
<td width="300" class="aaa">做对<%=q %>题 正确率:<%=q/(q+p)*100 %>%</td>
<td width="200" class="aaa">做错<%=p %>题</td>
</tr>
</table>
<form id="form1" name="form1" method="post" action="index.jsp">
<p>&nbsp;</p>
<input type="submit" name="tijiao" id="tijiao" value="继续答题" />
</form>
</body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP ‘time.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>


<html>
    <head>
      <meta charset="utf-8">
      <script type="text/javascript">
        var time = 8; //时间,秒
        function Redirect() {
        window.location = "MyJsp.jsp";
        }
        var i = 0;
        function dis() {
        document.all.s.innerHTML = "还剩" + (time - i) + "秒";
        i++;
        }
        timer = setInterval(‘dis()‘, 1000); //显示时间
        timer = setTimeout(‘Redirect()‘, time * 1000); //跳转
      </script>
  </head>
  <body>
    <span id="s"></span>
  </body>
</html>

 

 

(三)运行结果截图:

 

 

 

(四)编程总结:

组队过程中,别人比自己更容易去发现自己代码里的漏洞,在设计方面,两个人一起总结互相的意见中的有点,相对一个人的意见容易走歪路而言两个人更能想到的客户的需求,以及页面的布局与功能也随着两个人的讨论而越来越优化。

(五)开发过程时间记录日志:

技术分享图片

 

四则运算2的设计思想+源程序代码+运行结果截图+编程总结分析

标签:gets   页面   trace   generate   ==   span   个人   pre   while   

原文地址:http://www.cnblogs.com/837634902why/p/7994438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!