zoukankan      html  css  js  c++  java
  • 四则运算2(最终版)java+jps+sqlServer

    1,设计思想

    (1)java Resources里建立包和类

    (2)在类里面写入方法,其中包括生成算式create()和删除算式delete()用来更新数据库中的题目

    (3)Show()方法用来随机抽取数据库中的算式并显示

    (4)Caculate()方法用来接收参数,验证计算的结果

    (5)建立jsp文件,搭建网页的布局,调用java方法

    2,源程序代码

     java代码

    package com.jaovo.msg.model;
    
    public class Equation {
       int firstNum;
       int secondNum;
       String symbol;
       String id;
       int result;
       public String s[]={"+","-","*","/"};
       
    public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    
    public int getFirstNum() {
        return firstNum;
    }
    public void setFirstNum(int firstNum) {
        this.firstNum = firstNum;
    }
    public int getSecondNum() {
        return secondNum;
    }
    public void setSecondNum(int secondNum) {
        this.secondNum = secondNum;
    }
    public int getResult() {
        return result;
    }
    public void setResult(int result) {
        this.result = result;
    }
        
    }
    package com.jaovo.msg.dao;
    
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import com.jaovo.msg.Util.DBUtil;
    import com.jaovo.msg.Util.UserException;
    import com.jaovo.msg.model.Equation;
    import java.io.*;
    
    public class EquationDaoImpl  {
    
        public void create() {
            // TODO Auto-generated method stub
            Equation equ=new Equation();
            Random ran=new Random();
            for(int i=1;i<=1000;i++){
                Connection con=DBUtil.getConnection();
                equ.setFirstNum(ran.nextInt(0+100));
                equ.setSecondNum(ran.nextInt(0+100));
                equ.setSymbol(equ.s[ran.nextInt(4)]);
                if(i<10)
                equ.setId("000"+i);
                if(i>=10&&i<100)
                    equ.setId("00"+i);
                if(i>=100&&i<1000)
                    equ.setId("0"+i);
                if(i==1000)
                    equ.setId("1000");
                if(equ.getSymbol()=="+")
                    equ.setResult(equ.getFirstNum()+equ.getSecondNum());
                if(equ.getSymbol()=="-")
                    equ.setResult(equ.getFirstNum()-equ.getSecondNum());
                if(equ.getSymbol()=="*")
                    equ.setResult(equ.getFirstNum()*equ.getSecondNum());
                if(equ.getSymbol()=="/"&&equ.getSecondNum()!=0&&(equ.getFirstNum()%equ.getSecondNum()==0)&&equ.getFirstNum()>=equ.getSecondNum())
                    equ.setResult(equ.getFirstNum()/equ.getSecondNum());
                {
                    if(equ.getResult()<100&&equ.getResult()>=0){
                    try{
                        String sql="insert into equation(id,firstNum,symbol,secondNum,result)values('"+equ.getId()+"','"+equ.getFirstNum()+"','"+equ.getSymbol()+"','"+equ.getSecondNum()+"','"+equ.getResult()+"')";
                        Statement stmt=con.createStatement();
                        stmt.executeUpdate(sql);//更新
                    }catch(SQLException e){
                        e.printStackTrace();
                    }finally {
                        DBUtil.close(con);
                    }
                }
                else i--;
                    } 
            }
        }
        public void delete(){
            Equation equ=new Equation();
            Connection con = DBUtil.getConnection();
            for(int i=0;i<1000;i++){
                equ.setId(i+1+"");
                try {
                    Statement stmt=con.createStatement();
                    String sql = "delete from equation where id = "+Integer.parseInt(equ.getId());
                    stmt.executeUpdate(sql);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        public String show(){
            Equation equ=new Equation();
            Connection con=DBUtil.getConnection();
            String str ="";
            Random ran=new Random();
            Statement stmt;
            try {
                stmt = con.createStatement();
                int id1=ran.nextInt(999)+1;
                ResultSet rs=stmt.executeQuery("SELECT * FROM equation where id="+id1);
                while(rs.next()){
                    str=rs.getString("id")+" "+rs.getString("firstNum")+rs.getString("symbol")+rs.getString("secondNum")+"=";
                }
            } catch (SQLException e) {
                
                e.printStackTrace();
            }
            
            return str;
        }
        public String show1(String id1){
            Equation equ=new Equation();
            Connection con=DBUtil.getConnection();
            String str ="";
            Statement stmt;
            try {
                stmt = con.createStatement();
                ResultSet rs=stmt.executeQuery("SELECT * FROM equation where id="+id1);
                while(rs.next()){
                    str=rs.getString("firstNum")+rs.getString("symbol").trim()+rs.getString("secondNum")+"="+rs.getString("result");
                }
            } catch (SQLException e) {
                
                e.printStackTrace();
            }
    //        BufferedWriter bw;
    //        try {
    //            bw = new BufferedWriter(new FileWriter("wrong.txt",true));
    //            bw.write("nlc");
    //            bw.close();
    //        } catch (IOException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
            return str;
        }
        public String caculate(String result,String id){
            Equation equ=new Equation();
            Connection con=DBUtil.getConnection();
            String str = "";
            int result1=Integer.parseInt(result);
            int id1=Integer.parseInt(id);
            Statement stmt;
            try {
                stmt = con.createStatement();
                ResultSet rs=stmt.executeQuery("SELECT * FROM equation where id = "+id1);
                while(rs.next()){
                    if((rs.getInt("result"))==result1)
                        {str="正确";}
                    if((rs.getInt("result"))!=result1)
                        str="错误";
                }
                
            } catch (SQLException e) {
                
                e.printStackTrace();
            }
            return str;
        }
    }

    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=ISO-8859-1">
    <title>四则运算</title>
    </head>
    <center>
    <body background="D:图片动漫20150422H2607_fKHLB.jpeg">
        <%if(request.getAttribute("error1")!=null){
            out.print("<script language='javaScript'>alert('题数和时间不能为空'); </script>)");
            }
            %>
        <%if(request.getAttribute("error2")!=null){ 
            out.print("<script language='javaScript'>alert('请正确输入'); </script>)");
        }
        %>
        <center>
            <font color="red" size="4" face="楷体">欢迎来到四则运算</font>
        </center>
        <form action="caculateInput.jsp" method="post">
            <table align="center" >
            <tr>
                <td>
                    <font face="楷体">请输入出题数目</font>
                </td>
                <td>
                <input type="text" name="number" placeholder="请输入……">
                </td>
            </tr>
             <tr>
                <td>
                    <font face="楷体">请设置做题时间(秒)</font>
                </td>
                <td>
                <input type="text" name="time" placeholder="请输入……">
                </td>
            </tr>
            <tr align="center">
                    <td colspan="2">
                    <input type="submit" value="开始答题"/>
                    <input type="reset" value="重置题目数目"/>
                    </td>
                </tr>
            </table>
        </form>
    </body>
    </center>
    </html>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
     <%@page import="com.jaovo.msg.Util.showAnswer"%>
     <%@page import="com.jaovo.msg.dao.EquationDaoImpl" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%    
          String url = "http://localhost:8080/四则运算2/equation/start.jsp";   //设置跳转的地址  
          int time=Integer.parseInt(request.getParameter("time"));
          int number=Integer.parseInt(request.getParameter("number"));
          EquationDaoImpl equ=new EquationDaoImpl();
          String time1=request.getParameter("time");
          String number1=request.getParameter("number");
    %> 
        <%if("".equals(time1.trim())||"".equals(number1.trim())){
            request.setAttribute("error1", "题数和时间不能为空");
        %>
            <jsp:forward page="start.jsp"></jsp:forward>
        <%
        }
        %>
        
        <%
        if(time<0||number<0){
            request.setAttribute("error2", "请正确输入");
        
        %>
        <jsp:forward page="start.jsp"></jsp:forward>
       <%
        }
       %>
    <html>
    <head >
    <title>四则运算</title>
    <meta http-equiv=refresh content=<%=time %>;url=<%=url %>>  
    </head>
    <body background="D:图片新建文件夹9252150_130954294310_2.jpg" background-size:cover>
    <form action="caculate.jsp" method="post">
        <table align="center" >
                <%
                for(int i=0;i<number;i++){
                    String str=equ.show();
                %>
                <tr>
                    <td><%=str.substring(6, str.length()) %></td>
                    <td>
                        <input type="text" name="result" placeholder="请输入……" >
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="hidden" name="id" value="<%=str.substring(0,4) %>" >
                    </td>
                </tr>
                <%
                }
                %>
            <div align="center">  
                <h3 style=color:red>答题剩余时间:<span id=jump ></span></h3>  
                <h4><a href="start.jsp" onclick="jumpToIndex()">重新设题</a></h4>  
            </div> 
                <tr align="center">
                    <td colspan="2">
                    <input type="submit" value="提交"/>
                    <input type="reset" value="重置"/>
                    </td>
                </tr>
        </table>
    </form>
    </body>
            <script>  
                 function countDown(secs){  
                           jump.innerText=secs;  
                           if(--secs>0)  
                                    setTimeout("countDown("+secs+" )",1000);
                           if(secs==0)
                               document.forms[0].submit();
                 }
                 countDown(<%=time%>);        
                function jumpToIndex(){  
                    window.location.href='<%="http://localhost:8080/四则运算2/equation/start.jsp" %>';  
                }  
           </script>
    </html>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%@page import="java.util.*" %>
        <%@page import="com.jaovo.msg.dao.EquationDaoImpl" %>
    <!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=ISO-8859-1">
    <title>计算结果</title>
    </head>
    <body background="D:图片新建文件夹9252150_130954294310_2.jpg">
        <%
           String[] result=request.getParameterValues("result");
           String[] id=request.getParameterValues("id");
           String[] idNum=new String[100];
           int I=0;
           for(String i:id){
               idNum[I]=i;
               I++;
           }
           String[] RCaculate=new String[100];
           int x=0;
           String[] WCaculate=new String[100];
           int y=0;
           String[] KCaculate=new String[100];
           int z=0;
           I=0;
           EquationDaoImpl equDao=new EquationDaoImpl();
           for(String str:result){
               if(str == null || "".equals(str.trim())){
                   KCaculate[z]=equDao.show1(idNum[I]);z++;continue;
                   }
               if(equDao.caculate(str,idNum[I]).equals("正确")){
                   RCaculate[x]=equDao.show1(idNum[I]);x++;
                }
               if(equDao.caculate(str,idNum[I]).equals("错误")){
                   WCaculate[y]=equDao.show1(idNum[I]);y++;
               }
               I++;
           }
          out.println("正确的数量:"+x);
        %>
        <br/>
        <%
        out.print("错误的题目数量:"+y);
        %>
    <!--     <a href="wrong.jsp">查看错题</a> -->
        <br/>
        <%
        out.print("未答的题目数量:"+z);
        %>
    <!--     <a href="">查看未答的题目</a> -->
        <br/>
        <a href="create.jsp">更新题目</a><br/>
        <a href="start.jsp">重新计算</a><br/>
        <form action="wrong.jsp" method="post">
            <table align="center">
                <%
                    for(int i=0;i<y;i++){
                %>
                <tr>
                    <td>
                        <input type="hidden" name="wrong" value="<%=WCaculate[i]%>" >
                    </td>
                </tr>
                <%} %>
                <%
                for(int i=0;i<z;i++){ 
                %>
                <tr>
                    <td>
                        <input type="hidden" name="kong" value="<%=KCaculate[i]%>" >
                    </td>
                </tr>
                <%} %>
                <tr>
                    <input type="submit" value="查看错题和未做的题"/>
                </tr>
            </table>
        </form>
        
    </body>
    </html>
    <%@ 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>错题</title>
    </head>
    <body background="D:图片新建文件夹9252150_130954294310_2.jpg">
        <%
            String[] wrong=request.getParameterValues("wrong");
            String[] kong=request.getParameterValues("kong");
            out.println("错题");
        %>
        <br/>
        <%
            for(String w:wrong){
                out.println(w);
        %>
        <br/>
        <%
            }
            out.print("未做的题");
        %>
        <br/>
        <%
            for(String k:kong){
                out.print(k);
        %>
        <br/>
        <%
            }
        out.print("那你是很棒棒");
        %>
        <a href="start.jsp">再来一次 </a>
    </body>
    </html>
      <%@page import="com.jaovo.msg.dao.EquationDaoImpl" %>
    <%@ 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=ISO-8859-1">
    <title>创建题目</title>
    </head>
    <body>
        <h4 align="center">更新题目库成功</h4>
        <%
        EquationDaoImpl equDao=new EquationDaoImpl();
        equDao.delete();
        equDao.create();
        %>
        <div align="center">
        <a href="start.jsp">继续做题</a>
        </div>
    </body>
    </html>

    3,运行结果截图

     

    4,编程总结分析

    在jsp页面中尽量少使用java代码,养成良好习惯,应该在java中写入方法,然后调用即可

    在定义变量和方法时,应该根据具体情况来起名,增减参数,定义返回值类型

    5,时间记录日志

    日期 开始时间 结束时间 中断时间 净时 活动 备注 C U
    12/1 9:00 15:40 2个小时 4小时40分钟 编程序 四则运算,休息    
    12/2 9:00 17:00 4个小时 4小时 编程序 四则运算,休息    
    12/3 9:00 11:00 0 2小时 编程序 四则运算    
    12/4 16:00 17:00 0 2小时 编程序 四则运算    
    12/5 8:00 12:00 0 4小时 上课 软件工程概论,数据结构    
    Personal Software Process Stages   time
    计划  
    估计这个任务需要的时间 3天
    开发  
    需求分析(包括学习新技术) 传参数(结果集)
    生成设计文档
    设计复审(和同事审核设计文档)
    代码规范(为目前的开发制定合适的规范) 首行缩进,大括号另起一行
    具体设计 在jsp中调用java方法,将结果在页面上显示
    具体编码 参考上面(两天)
    代码复审 每次修改后都会检查
    测试(自我测试,修改代码,提交修改) 半天
    报告  
    测试报告 1个小时
    计算工作量 3天
    事后总结,并提出过程改进计划 无法记录错题,改进计划:将错题保存在数据库中
    合计 3天

    10个测试用例

    1,能出题   2,能倒计时 3,能判断错对 4,能将错题显示出来 5,能判断是否有空题 6,能更新题目(数据库)7,能重新出题 8,能判断不能输入负数 

    9,能出多道题目10,能重新计算错题

    进过验证以上功能都能实现

    学到了要灵活运用各种自定义的方法,学会给jsp页面布局,能灵活运用数据库

  • 相关阅读:
    怎样才能算是在技术上活跃的小公司
    jquery幻灯片--渐变
    cpm效果介绍
    我依然热爱编程
    项目开发经验终结2015/4/7
    windows上putty访问ubuntu
    ubuntu安装openssh-server
    今天犯了一个低级错误
    linux 搭建lamp环境
    能用存储过程的DBHelper类
  • 原文地址:https://www.cnblogs.com/news1997/p/7994302.html
Copyright © 2011-2022 走看看