四则运算2

冷暖自知 提交于 2020-01-31 06:16:14

设计思想:

先在数据库中存好1000道题,编号序号,题的出法用随机数,出题时判断答案是否符合要求,如果符合则添加到数据库中。然后用户在jsp界面中输入出题的数量,然后随机生成改数量的随机数作为编号,从数据库中调题,分别把数据赋给number对象,显示到jsp界面中。

源代码:

package com.jaovo.msg.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.model.Allnumber;


public class NumberDao extends Allnumber {
    public Allnumber load(int id) {
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "select * from table_data  where id = ?";
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Allnumber allnumber = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                allnumber = new Allnumber();
                allnumber.setFirstnumber(resultSet.getInt("firstnumber"));
                char fuhao=resultSet.getString("fuhao").charAt(0);
                allnumber.setFuhao(fuhao);
                allnumber.setSecondnumber(resultSet.getInt("secondnumber"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        return  allnumber;
    }
    public int loadAnswer(int id) {
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "select * from table_data  where id = ?";
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Allnumber allnumber = null;int answer = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                allnumber = new Allnumber();
                allnumber.setFirstnumber(resultSet.getInt("firstnumber"));
                char fuhao=resultSet.getString("fuhao").charAt(0);
                allnumber.setFuhao(fuhao);
                allnumber.setSecondnumber(resultSet.getInt("secondnumber"));
                answer=allnumber.Get_answer(allnumber.getFirstnumber(),allnumber.getSecondnumber(),allnumber.getFuhao());
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        return  answer;
    }
    public void add(Allnumber allnumber)
    {
    //获得连接对象
    Connection connection = DBUtil.getConnection();
    //创建语句传输对象
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
        String sql="insert into table_user(firstnumber,fuhao,secondnumber,answer,state) values (?,?,?,?,?)";
        String fuhao=""+allnumber.getFuhao();
        
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1, allnumber.getFirstnumber());
            preparedStatement.setString(2, fuhao);
            preparedStatement.setInt(3, allnumber.getSecondnumber());
            
            preparedStatement.setInt(4, allnumber.Get_answer(allnumber.getFirstnumber(), allnumber.getSecondnumber(), allnumber.getFuhao()));
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
        //关闭资源
        DBUtil.close(resultSet);
        DBUtil.close(preparedStatement);
        DBUtil.close(connection);
        }
        
        
    }
    

}
数据库操作
package com.jaovo.msg.model;

import java.util.Random;

public class Allnumber {
    private int firstnumber;
    private int secondnumber;
    private char fuhao;
    public void Set_data()
    {
        Random ran= new Random();
        int number1=ran.nextInt(100);setFirstnumber(number1);
        int number2=ran.nextInt(100);
        while(number2==0)number2=ran.nextInt(100);
        setSecondnumber(number2);
        int fuhao=ran.nextInt(4);
        switch(fuhao){
        case(0):setFuhao('+');break;
        case(1):setFuhao('-');break;
        case(2):setFuhao('*');break;
        case(3):setFuhao('/');break;
        }
    }
    public int Get_answer(int firstnumber,int secondnumber,char fuhao)
    {
        int answer=0;
        switch(fuhao){
        case('+'):answer=firstnumber+secondnumber;break;
        case('-'):answer=firstnumber-secondnumber;break;
        case('*'):answer=firstnumber*secondnumber;break;
        case('/'):answer=firstnumber/secondnumber;break;
        }
        return answer;
    }
    public int getFirstnumber() {
        return firstnumber;
    }
    public void setFirstnumber(int firstnumber) {
        this.firstnumber = firstnumber;
    }
    public int getSecondnumber() {
        return secondnumber;
    }
    public void setSecondnumber(int secondnumber) {
        this.secondnumber = secondnumber;
    }
    public char getFuhao() {
        return fuhao;
    }
    public void setFuhao(char fuhao) {
        this.fuhao = fuhao;
    }

}
Allnumber类
<%@page import="com.jaovo.msg.Util.EmptyUtil"%>
<%@ 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="C:\Users\张强\Pictures\Saved Pictures/1505615532655.jpg">
       <form action="chuti.jsp" method="get">
       <table align="center" border="1" width="500">
       <tr>
       <div align="center"color="red"  >
       <h3>哒哒哒哒四则运算</h3>
       </div>
       </tr>
       <tr>
                <td>请输入要做的题数 : </td>
                <td>
                    <input type="text" name="sum" />
                    <%=EmptyUtil.showError(request,"sum") %>
                </td>
        </tr>
         <tr>
                <td>请输入需要几分钟 : </td>
                <td>
                    <input type="text" name="time" />
                    <%=EmptyUtil.showError(request,"time") %>
                </td>
        </tr>
        <tr align="center">
                <td colspan="2">
                    <input type="submit" value="提交" />
                </td>
            </tr>
            </table>
    </form>

</body>
</html>
界面jsp文件
<%@page import="com.jaovo.msg.Dao.NumberDao"%>
<%@page import="com.jaovo.msg.Util.EmptyUtil"%>
<%@page import="java.util.List"%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.Random"%>
<%@page import="com.jaovo.msg.model.Allnumber"%>
<%@ 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="C:\Users\张强\Pictures\Saved Pictures/1505615532655.jpg">
<div align="center"color="red" id="timer"align="center"> 
      <%
    //接收客户端传递过来的参数
    
      String sum = request.getParameter("sum");
      String time = request.getParameter("time");
      boolean validate =EmptyUtil.validateNull(request, new String[]{"sum","time"});
      if(!validate){
  %>
      <jsp:forward page="jiemain.jsp"></jsp:forward>
  <%
      }
      %>
      <form  action="pnaduan.jsp" method="get">
       <table align="center" border="1" width="500">
       <%
       //设置出题数目,并从数据库中抽出题目,赋值给AllNumber
       int newsum=Integer.parseInt(sum);
       int time2=Integer.parseInt(time);
       int t=time2*60000;
       List<Allnumber> AllNumber=new LinkedList<Allnumber>();
       int question=0;
        for(int i=0;i<newsum;i++)
        {
            NumberDao numberdao=new NumberDao();
            Random ran=new Random();
            question=ran.nextInt(1020);
            AllNumber.add(numberdao.load(question));
            session.setAttribute("sum",sum);
            
        }
        session.setAttribute("allnumber",AllNumber);
        %>
        <center>剩余时间:<span id=id2>0</span>秒;</center>
        <script language="javascript">
           var i=<%=time2*60%>;
          function showtime(){
          i=i-1;
          id2.innerHTML=i;
          setTimeout("showtime()",1000);
          if(i==<%=0%>)
            document.forms[0].submit();
        
            }
          showtime();
        </script>
        <%
        
        for(int i=0;i<AllNumber.size();i++)
        {
            Allnumber number=new Allnumber();
            number=AllNumber.get(i);
            out.print(number.getFirstnumber());
            out.print(number.getFuhao());
            out.print(number.getSecondnumber()+"=");
        %>
       <input name="answer"/><br></br>
        <%    
        }
        %>
        <tr align="center">
                <td colspan="2">
                    <input type="submit" value="提交" />
                </td>
            </tr>
       </table>
       </form>
      </div>
</body>
</html>
出题jsp文件

结果截图:

时间记录日志:

Personal Software Process Stages

Time

计划

 

  · 估计这个任务需要多少时间

 4个小时

开发

 

  · 需求分析 (包括学习新技术)

 25分钟

  · 生成设计文档

 10分钟

  · 设计复审 (和同事审核设计文档)

 无

  · 代码规范 (为目前的开发制定合适的规范)

 10分钟

  · 具体设计

20分钟

  · 具体编码

 1小时  

  · 代码复审

 30分钟

  · 测试(自我测试,修改代码,提交修改)

3小时 

报告

 

  · 测试报告

 无

  · 计算工作量

 无

  · 事后总结, 并提出过程改进计划

 1小时

合计

5小时35分钟

 

 

 测试用例

(1)测试100个题目

(2)测试1000个题目

(3)测试10000个题目

(4)测试100000个题目

为的是测试出题上限是多少。

(5)不输入直接点提交测试

(6)输入负数测试

(7)输入字符串

(8)输入特殊字符

正确性

用户输入任何都不会导致报错,有足够健壮性

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!