博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
四则运算1
阅读量:5280 次
发布时间:2019-06-14

本文共 9519 字,大约阅读时间需要 31 分钟。

设计思想:在.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()

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

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" %>小学100以内四则运算界面<%--     <%Map
errorMsg = (Map
)request.getAttribute("errormsg"); --%><%-- %> --%><% ChangeType change=new ChangeType(); String []math=new String[30]; math=change.changetype(); %>
<% String []result=new String[30]; for(int i=0;i<30;i++) {%>
<% } %>
<% out.println(math[i]);%>

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"%><%     //接收客户端传递过来的参数    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="×";        }    %>        

答案是:<% out.print(kresult[i]); out.print("你的结果:"+result1[i]+"是"+string+"的");%>

<% } %>

 

转载于:https://www.cnblogs.com/mqlblog/p/7994256.html

你可能感兴趣的文章
Delphi函数动态调用实现免杀
查看>>
字符串的处理常用方法
查看>>
【加密算法】MD5
查看>>
sql无效字符 执行sql语句报错解决方案
查看>>
12年程序员职业生涯得到的12个经验教训
查看>>
python+基本3D显示
查看>>
Swift学习笔记(7):函数
查看>>
编程网站推荐
查看>>
解决Apache的错误日志巨大的问题以及关闭Apache web日志记录
查看>>
BZOJ 1123: [POI2008]BLO
查看>>
数据库连接 中的测试事例,包括工具类,配置文件,查询,增加,以及查询后返回对象...
查看>>
Android快乐贪吃蛇游戏实战项目开发教程-03虚拟方向键(二)绘制一个三角形
查看>>
数据挖掘-集成学习
查看>>
* 和 ** python
查看>>
oracle 存储过程第四天
查看>>
【转】字符流和字节流的区别,使用场景,相关类
查看>>
代理模式
查看>>
第5章 C++STL泛化技术分析
查看>>
CentOS7 安装 Nginx
查看>>
afreechart
查看>>