zoukankan      html  css  js  c++  java
  • 四则运算1

    设计思想:在.java文件中创建随机数,组成一个运算式,计算答案为String类型,方便分数的判断。传递到jsp页面中进行判断,判断输入结果与正确结果是否相匹配,提交以后显示题目的正确结果。

    源代码:

    Add.java

    package com.jaovo.msg.dao;
    
    import com.jaovo.msg.model.Math_Message;
    
    public interface Add {
        public void add(Math_Message math);
    
    }

    ChangeType.java

    package com.jaovo.msg.dao;
    import com.jaovo.msg.dao.Count;
    import  com.jaovo.msg.dao.Mathimpl;
    import com.jaovo.msg.model.Math_Message;
    public class ChangeType {
        public String [] changetype()
        {
            String []change=new String[30];
            for(int i=0;i<30;i++)
            {
                Math_Message math=new Math_Message();
                Count count=new Count();
                math=count.count();
                Mathimpl add=new Mathimpl();
                if(math.getResult().length()>2)
                {
                    if(math.getNumber1()<math.getNumber2())
                    {
                        if(math.getOperator().equals("÷"))
                        {
                        change[i]=Integer.toString(math.getNumber1())+math.getOperator()+Integer.toString(math.getNumber2())+"=";    
                        add.add(math);//将一个正确的算术式添加到数据库
                        }
                        else
                        {
                            i=i-1;
                        }        
                    }
                    else
                    {
                        i=i-1;
                    }    
                }
                else if(Integer.parseInt(math.getResult())<0)
                {
                    i=i-1;
                    
                }
                else 
                {
                change[i]=Integer.toString(math.getNumber1())+math.getOperator()+Integer.toString(math.getNumber2())+"=";    
                add.add(math);//将一个正确的算术式添加到数据库
                }
                
            }
            return change;
            
        }
    
    }

    Count .java

    package com.jaovo.msg.dao;
    import com.jaovo.msg.model.Math_Message;
    public class Count {
        //计算最大公约数函数方法
         public static int maxCommonDivisor2(int m, int n) {  
             if (m < n) {// 保证m>n,若m<n,则进行数据交换  
                 int temp = m;  
                 m = n;  
                 n = temp;  
             }  
             while (m % n != 0) {// 在余数不能为0时,进行循环  
                 int temp = m % n;  
                 m = n;  
                 n = temp;  
             }  
             return n;// 返回最大公约数  
         }  
        //生成随机数并计算出结果
        public     Math_Message count()
        {
            int number1,number2=0;//暂时存放产生的两个随机数,以便于计算正确结果
            int operator;//产生0-3的随机数代表四个运算符
            String result;//计算出正确结果
            number1=(int) (Math.random()*100);
            while(number2==0)
            {
            number2=(int) (Math.random()*100);
            }
            Math_Message math=new Math_Message() ;
            math.setNumber1(number1);
            math.setNumber2(number2);
            operator=(int) (Math.random()*4);
            if(operator==0)
            {
                math.setOperator("+");
                result=Integer.toString(number1+number2);
                math.setResult(result);
                        
            }
            else if(operator==1)
            {
                math.setOperator("-");
                result=Integer.toString(number1-number2);
                math.setResult(result);
            }
            else if(operator==2)
            {
                math.setOperator("*");
                result=Integer.toString(number1*number2);
                math.setResult(result);
            }
            else if(operator==3)
            {
                if(number1%number2==0)//可以除尽
                {
                    math.setOperator("÷");
                    result=Integer.toString(number1/number2);
                    math.setResult(result);
                }
                else //无法除尽的结果
                {
                    int pubnumber;
                    math.setOperator("÷");
                    //求最大公约数
                    pubnumber= maxCommonDivisor2(number1,number2);
                    number1=number1/pubnumber;
                    number2=number2/pubnumber;
                    result=Integer.toString(number1)+"/"+Integer.toString(number2);
                    math.setResult(result);
                    
                }
                
                
                
            }
            
            return math;
            
            
        }
        
    
    }

    Mathimpl.java

    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.model.Math_Message;
    import com.jaovo.msg.Util.DBUtil;
    
    public class Mathimpl implements Add{
    
        @Override
        public void add(Math_Message math) {
            //获得链接对象
                    Connection connection = DBUtil.getConnection();
                    //准备sql语句
                    String sql = "select count(*) from math_user where number1 = ?";
                    //创建语句传输对象
                    PreparedStatement preparedStatement = null;
                    ResultSet resultSet = null;
                    try {
                        
                        
                        sql = "insert into math_user(number1,number2,operator,result) value (?,?,?,?)";
                        preparedStatement = connection.prepareStatement(sql);
                        preparedStatement.setInt(1, math.getNumber1());
                        preparedStatement.setInt(2, math.getNumber2());
                        preparedStatement.setString(3,math.getOperator());
                        preparedStatement.setString(4, math.getResult());
                        preparedStatement.executeUpdate();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }finally {
                        //关闭资源
                        DBUtil.close(resultSet);
                        DBUtil.close(preparedStatement);
                        DBUtil.close(connection);
                    }
                    
            
        }
        
    
    }

    Read.java

    package com.jaovo.msg.dao;
    
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    
    public class Read {
    
        public  String[] YunSuanResult() {
            java.sql.Connection connection=null;
            String user = "root";
            String password="root";
            int i=0;
            String[] a=new String[30];
            String url="jdbc:mysql://localhost:3306/jaovo_msg";
            String qingchu="truncate table math_user";
                try {
                     Class.forName("com.mysql.jdbc.Driver").newInstance();
                     
                } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                     connection = DriverManager.getConnection(url,user,password);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                PreparedStatement preparedStatement=null;
                ResultSet resultSet=null;
            
                
                String operation="select * from  math_user order by id";
                try {
                    preparedStatement=connection.prepareStatement(operation);
                    resultSet=preparedStatement.executeQuery();
                    while(resultSet.next()) {
                        a[i]=resultSet.getString("result");
                        i++;
                    }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
        }    
                 try {
                        preparedStatement=connection.prepareStatement(qingchu);
                        preparedStatement.executeUpdate();
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
            finally {try {
            resultSet.close();
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }    
                
            return a;
    
    }
    }

    Math_Message.java

    package com.jaovo.msg.model;
    
    public class Math_Message {
        private int number1;
        private int number2;
        private String operator;
        private String result;
        public String getResult() {
            return result;
        }
        public void setResult(String result) {
            this.result = result;
        }
        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 getOperator() {
            return operator;
        }
        public void setOperator(String operator) {
            this.operator = operator;
        }
    
    
    }

    DBUtil.java

    package com.jaovo.msg.Util;
    import java.sql.*;
    public class DBUtil {
        
        public  static  Connection getConnection() {
            try {
                //1 ��������
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String user = "root";
            String password = "root";
            String url = "jdbc:mysql://localhost:3306/jaovo_msg";
            Connection connection = null;
            try {
                //2 �������Ӷ���connection
                 connection = DriverManager.getConnection(url,user,password);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return connection;
        }
        
        //�ر���Դ�ķ���
        public static void close(Connection connection ) {
            try {
                if (connection != null) {
                    connection.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(PreparedStatement preparedStatement ) {
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(ResultSet resultSet ) {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    UserException.java

    package com.jaovo.msg.Util;
    
    public class UserException extends RuntimeException{
    
        public UserException() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
            // TODO Auto-generated constructor stub
        }
    
        public UserException(String message, Throwable cause) {
            super(message, cause);
            // TODO Auto-generated constructor stub
        }
    
        public UserException(String message) {
            super(message);
            // TODO Auto-generated constructor stub
        }
    
        public UserException(Throwable cause) {
            super(cause);
            // TODO Auto-generated constructor stub
        }
        
    }

    show.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@page import="com.jaovo.msg.dao.ChangeType" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>小学100以内四则运算界面</title>
    </head>
    <body>
    <%--     <%Map<String,String> errorMsg = (Map<String,String>)request.getAttribute("errormsg"); --%>
    <%--     %> --%>
    <%
        ChangeType change=new ChangeType();
        String []math=new String[30];
        math=change.changetype(); 
    %>
        <form action="showInput.jsp" method="get">
            <table align="center" border="1" width="500">
    <% 
            String []result=new String[30];
               for(int i=0;i<30;i++)    
               {
    %>   
                  <tr>
                    <td><% out.println(math[i]);%></td>
                    <td>
                        <input type="text" name="result">
                    </td>
                </tr>
    <%   
               }
                    
    %>
                <tr align="center">
                    <td>
                        <input type="submit" value="提交" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
    </html>

    showInput.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%@page import="com.jaovo.msg.Util.DBUtil" import=" java.sql.*" import="com.jaovo.msg.Util.UserException" import="com.jaovo.msg.dao.Read"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    
    <% 
        //接收客户端传递过来的参数
        String []result1=new String[30];
        result1 = request.getParameterValues("result");
        String string=" ";
        String []kresult=new String[30];
        Read read=new Read();
        kresult=read.YunSuanResult();
        for(int i=0;i<30;i++)
        {
            if(result1[i].equals(kresult[i]))
            {
                string="√";
            }
            else
            {
                string="×";
            }
        %>
            <h2> 答案是:<% out.print(kresult[i]); out.print("你的结果:"+result1[i]+"是"+string+"的");%></h2>
        <% 
        }
        
    %>
    </html>
  • 相关阅读:
    poj1125传播谣言(弗洛伊德,求最长路)
    poj2253青蛙(可到达路径的单次跳跃最短距离)
    poj1860 兑换货币(bellman ford判断正环)
    poj1062 昂贵的礼物(dijkstra+枚举)
    hdu4081 秦始皇修路(次小生成树)
    梦想,永不停息!
    nofollow标签如何使用
    后台地址报错:Service Unavailable
    DEDECMS批量修改默认文章和列表命名规则的方法
    FreeOnTerminate 的线程在线程管理类的Destroy释放时手工释放的问题
  • 原文地址:https://www.cnblogs.com/mqlblog/p/7994256.html
Copyright © 2011-2022 走看看