zoukankan      html  css  js  c++  java
  • 软件工程个人作业01

    一.程序设计思想

    四则运算出题可以使用随机数分别生成两个运算数和四个运算符之一,计算出结果
    将两个运算数和运算符,结果存入数据库
    将数据库中的算式打印在页面上,将用户的答案与正确答案比对得出正误

    二.源代码

    package Util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class Util
    {
        public static Connection getConnection()
        {
            try
            {
                Class.forName("com.mysql.jdbc.Driver");
                
            }
            catch(ClassNotFoundException e)
            {
                e.printStackTrace();
            }
            String url="jdbc:mysql://localhost:3306/java?useSSL=false";
            Connection connection=null;
            try {
                connection=DriverManager.getConnection(url, "root", "root");
                
            } 
            catch (SQLException e) 
            {
                e.printStackTrace();
                System.out.println("数据库连接失败!");
            }
            return connection;
        }
        public static void close(Connection connection)
        {
            try
            {
                if(connection!=null)
                {
                    connection.close();
                }
            }catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        public static void close(PreparedStatement preparedStatement)
        {
            try
            {
                if(preparedStatement!=null)
                {
                    preparedStatement.close();
                }
            }catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        public static void close(ResultSet resultSet)
        {
            try
            {
                if(resultSet!=null)
                {
                    resultSet.close();
                }
            }catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
    
    }
    Util.java
    package Util;
    
    public class LingException extends Exception
    {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        public LingException()
        {
            super();
        }
        public LingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace)
        {
            super(message, cause, enableSuppression, writableStackTrace);
        }
        public LingException(String message, Throwable cause) 
        {
            super(message, cause);
        }
        public LingException(String message) 
        {
            super(message);
        }
        public LingException(Throwable cause)
        {
            super(cause);
        }
    }
    LingException
    package yunsuan;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import Util.Util;
    import yunsuan.shizi;
    public class biaodashizu
    {
        public void create(int j)
        {
            Connection connection=Util.getConnection();
            PreparedStatement preparedStatement=null;
            ResultSet resultSet=null;
            for(int i=0;i<j;i++)
            {
                shizi s=new shizi();
                String sql="insert into sizeyunsuan(id,number1,ch,number2,result) value(?,?,?,?,?)";
                try 
                {
                    preparedStatement=connection.prepareStatement(sql);
                    preparedStatement.setInt(1, i+1);
                    preparedStatement.setInt(2, s.number1);
                    preparedStatement.setString(3, s.ch);
                    preparedStatement.setInt(4, s.number2);
                    preparedStatement.setString(5, ""+s.result);
                    preparedStatement.executeUpdate();
                } 
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
                }
                    Util.close(connection);
                    Util.close(preparedStatement);
                    Util.close(resultSet);
        }
    
        public void delete()
        {
        Connection connection=Util.getConnection();
        String sql="delete from sizeyunsuan";
        PreparedStatement preparedStatement=null;
        try 
        {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.executeUpdate();
        } 
        catch (SQLException e) 
        {
            e.printStackTrace();
        }
        finally
        {
            Util.close(connection);
            Util.close(preparedStatement);
        }
        }
        public shizi showDan(int i)
        {
            Connection connection=Util.getConnection();
            PreparedStatement preparedStatement=null;
            ResultSet resultSet=null;
            String sql="select * from sizeyunsuan where id=?";
            shizi shi=null;
            try 
            {
                    preparedStatement=connection.prepareStatement(sql);
                    preparedStatement.setInt(1, i+1);
                    resultSet=preparedStatement.executeQuery();
                    while(resultSet.next())
                    {
                        shi=new shizi();
                        shi.setNumber1(resultSet.getInt("number1"));
                        shi.setCh(resultSet.getString("ch"));
                        shi.setNumber2(resultSet.getInt("number2"));
                        shi.setResult(resultSet.getInt("result"));
                    }
                } 
                catch (SQLException e) 
                {
                    e.printStackTrace();
                }
                    Util.close(connection);
                    Util.close(preparedStatement);
                    Util.close(resultSet);
            return shi;
        }
        public shizi showscore(int i)
        {
            Connection connection=Util.getConnection();
            PreparedStatement preparedStatement=null;
            ResultSet resultSet=null;
            String sql="select * from sizeyunsuan where id=?";
            shizi shi=null;
            try 
            {
                    preparedStatement=connection.prepareStatement(sql);
                    preparedStatement.setInt(1, i+1);
                    resultSet=preparedStatement.executeQuery();
                    while(resultSet.next())
                    {
                        shi=new shizi();
                        shi.setNumber1(resultSet.getInt("number1"));
                        shi.setCh(resultSet.getString("ch"));
                        shi.setNumber2(resultSet.getInt("number2"));
                        shi.setResult(resultSet.getInt("result"));
                        shi.setAnswer(resultSet.getInt("answer"));
                        shi.setStatus(resultSet.getInt("zhengwu"));
                    }
                } 
                catch (SQLException e) 
                {
                    e.printStackTrace();
                }
                    Util.close(connection);
                    Util.close(preparedStatement);
                    Util.close(resultSet);
            return shi;
        }
        public int getSum()
        {
            int n=0;
            Connection connection=Util.getConnection();
            String sql="select  id from sizeyunsuan";
            PreparedStatement preparedStatement=null;
            ResultSet resultSet=null;
            try 
            {
                preparedStatement=connection.prepareStatement(sql);
                resultSet=preparedStatement.executeQuery();
                while(resultSet.next())
                {
                        n++;
                }
            } 
            catch (SQLException e) 
            {
                e.printStackTrace();
            }
            return n;
        }
        public void xieru(int j,int result)
        {
            Connection connection=Util.getConnection();
            PreparedStatement preparedStatement=null;
            String sql="update sizeyunsuan set answer = ? where id = ?";
            try {
                preparedStatement=connection.prepareStatement(sql);
                preparedStatement.setInt(1, result);
                preparedStatement.setInt(2, j);
                preparedStatement.executeUpdate();
            }
            catch (SQLException e) 
            {
                e.printStackTrace();
            }
            finally
            {
                Util.close(connection);
                Util.close(preparedStatement);
            }
        }
        public void panduan(int j,int result)
        {
            Connection connection=Util.getConnection();
            PreparedStatement preparedStatement=null;
            ResultSet resultSet=null;
            String sql="select * from sizeyunsuan where id=?";
            shizi shi=null;
            try 
            {
                    preparedStatement=connection.prepareStatement(sql);
                    preparedStatement.setInt(1, j);
                    resultSet=preparedStatement.executeQuery();
                    while(resultSet.next())
                    {
                        shi=new shizi();
                        shi.setNumber1(resultSet.getInt("number1"));
                        shi.setCh(resultSet.getString("ch"));
                        shi.setNumber2(resultSet.getInt("number2"));
                        shi.setResult(resultSet.getInt("result"));
                        shi.setAnswer(resultSet.getInt("answer"));
                        shi.setStatus(0);
                    }
            } 
            catch (SQLException e) 
            {
                e.printStackTrace();
            }
                if(shi.getResult()!=result)
                {
                    shi.setStatus(2);
                }
                else
                {
                    shi.setStatus(1);
                }
                sql="update sizeyunsuan set zhengwu = ? where id = ?";
                try
                {
                    preparedStatement=connection.prepareStatement(sql);
                    preparedStatement.setInt(1, shi.getStatus());
                    preparedStatement.setInt(2, j);
                    preparedStatement.executeUpdate();
                }
                catch(SQLException e)
                {
                    e.printStackTrace();
                }
                    Util.close(connection);
                    Util.close(preparedStatement);
                    Util.close(resultSet);
        }
        
    }
    biaodashizu.java
    package yunsuan;
    import java.util.Random;
    public class shizi 
    {
        int number1;
        int number2;
        int fu;
        String ch;
        int result=-1;
        int answer;
        int status;
        public int getAnswer() {
            return answer;
        }
        public void setAnswer(int answer) {
            this.answer = answer;
        }
        public int getStatus() {
            return status;
        }
        public void setStatus(int status) {
            this.status = status;
        }
        public shizi()
        {
            Random ran=new Random();
            while(result<0||result>100)
            {
            number1=ran.nextInt(100)+1;
            number2=ran.nextInt(100)+1;
            fu=ran.nextInt(4);
            switch(fu)
            {
            case 0:
                ch="+";
                result=number1+number2;
                break;
            case 1:
                ch="-";
                result=number1-number2;
                break;
            case 2:
                ch="*";
                number1=ran.nextInt(9)+1;
                number2=ran.nextInt(9)+1;
                result=number1*number2;
                break;
            case 3:
                ch="/";
                while(number1%number2!=0)
                {
                    number1=ran.nextInt(100)+1;
                    number2=ran.nextInt(100)+1;
                }
                result=number1/number2;
                break;
            }
            }
        }
        public int getNumber1() {
            return number1;
        }
        public void setNumber1(int number1) {
            this.number1 = number1;
        }
        public int getNumber2() {
            return number2;
        }
        public void setNumber2(int number2) {
            this.number2 = number2;
        }
        public String getCh() {
            return ch;
        }
        public void setCh(String ch) {
            this.ch = ch;
        }
        public int getResult() {
            return result;
        }
        public void setResult(int result) {
            this.result = result;
        }
        
    }
    shizi.java
    <%@page import="yunsuan.biaodashizu" %>
    <%@ 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>
    <h2 style="color:blue" align="center">四则运算练习题</h2>
    <hr size=%40 color=blue>
        <form method="get" action="testmain.jsp">
        <table align="center" border="2" width="400">
        <tr>
        <td>要生成的题目数:</td>
        <td>
            <input type="text" name="num"/>
        </td> 
        </tr>
        <tr align="center">
        <td colspan="2">
            <input type="submit" name="提交" />
        </td>
        </tr>
        </table>
        </form>
    </body>
    </html>
    index.jsp
    <%@page import="yunsuan.biaodashizu" %>
    <%@ 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>
    <h2 style="color:blue" align="center">四则运算练习题</h2>
    <hr>
        <%
            biaodashizu biaodashi=new biaodashizu();
            if(request.getParameter("num")!=null&&!"".equals(request.getParameter("num").trim()))
            {
                biaodashi.delete();
                int x=Integer.parseInt(request.getParameter("num"));
                biaodashi.create(x);
            }
        %>
        <form method="get" action="testscore.jsp">
        <table align="center" border="2" width="500">
            <tr align="center">
            <td>
                                         题目总数为:
            </td>
            <td colspan="3">
                <input type="text" style="375px" name="num" value=<%=biaodashi.getSum()%> disabled />
            </td>
            </tr>
            <tr>
            <td>题目编号</td>
            <td>题目表达式</td>
            <td>答案输入</td>
            <td>答题情况</td>
            <tr>
            <%
            int x=biaodashi.getSum();
            for(int i=0;i<x;i++)
            {
            %>
                <tr>
                <td><%=i+1 %></td>
                <td><%="	"+biaodashi.showDan(i).getNumber1()+"	"+biaodashi.showDan(i).getCh()+"	"+biaodashi.showDan(i).getNumber2()+"	"+"=" %></td>
                <td>
                    <input type="text" name="text" />
                </td>
                <td>
                </td>
                </tr>
            <%
            }
            %>
            <tr align="center">
            <td colspan="4">
                <input type="submit" name="提交" />
            </td>        
            </tr>
        </table>
        </form>
    </body>
    </html>
    testmain.java
    <%@page import="yunsuan.biaodashizu" %>
    <%@ 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>
    <h2 style="color:blue" align="center">四则运算练习题</h2>
    <hr>
        <%
            biaodashizu biaodashi=new biaodashizu();
            String jieshou[]=request.getParameterValues("text");
            int TRUE=0,FALSE=0;
            for(int i=0;i<biaodashi.getSum();i++)
            {
                int res=Integer.parseInt(jieshou[i]);
                biaodashi.xieru(i+1,res);
                biaodashi.panduan(i+1,res);
            }
        %>
        <form method="get" action="testmain.jsp">
        <table align="center" border="2" width="500">
            <tr>
            <td>题目编号</td>
            <td>题目表达式</td>
            <td>提交答案</td>
            <td>答题情况</td>
            <tr>
            <%
            
            for(int i=0;i<biaodashi.getSum();i++)
            {
            %>
                <tr>
                <td><%=i+1 %></td>
                <td><%="	"+biaodashi.showDan(i).getNumber1()+"	"+biaodashi.showDan(i).getCh()+"	"+biaodashi.showDan(i).getNumber2()+"	"+"=" %></td>
                <td>
                    <%="	"+biaodashi.showscore(i).getAnswer() %>
                </td>
                <td>
                    <%
                        if(biaodashi.showscore(i).getStatus()==1)
                        {
                        %>
                            <img src="picture/正确.png">
                            <%TRUE++; %>
                        <%
                        }
                        else if(biaodashi.showscore(i).getStatus()==2)
                        {
                        %>
                            <img src="picture/错误.gif">
                            <h6 style="color:red">正确答案为:<%=biaodashi.showscore(i).getResult() %></h6>
                            <%FALSE++; %>
                        <%
                        }
                    %>
                </td>
                </tr>
            <%
            }
            %>
            <tr align="center">
            <td colspan="4">
                <h2>正确数:<%=TRUE %><%="	" %>错误数:<%=FALSE %></h2>
            </td>
            </tr>
            <tr align="center">
            <td colspan="4">
                <a href="index.jsp">重新开始练习</a>
            </td>        
            </tr>
        </table>
        </form>
    </body>
    </html>
    testscore.java

    三.运行结果

  • 相关阅读:
    spark源码解析1-master启动流程
    AngularJS Backbone.js Ember.js 对比
    Win 8下Rime输入法无法同步的临时解决方法
    osx上使用'cd'命令跳转到别名(alias)目录
    在JS中简单实现Formatter函数
    gvim 全屏 插件
    45 Useful JavaScript Tips, Tricks and Best Practices
    Javascript几个时髦的hack技巧《Javascript Hacks for Hipsters》
    关于如何参与到开源项目中《How To Succeed In Open Source ( In Ways You Haven't Considered Yet )》
    mac 下launchpad超级慢的问题
  • 原文地址:https://www.cnblogs.com/sdysyhj/p/7994939.html
Copyright © 2011-2022 走看看