设计思想:在.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以内四则运算界面 <%-- <%MaperrorMsg = (Map )request.getAttribute("errormsg"); --%><%-- %> --%><% ChangeType change=new ChangeType(); String []math=new String[30]; math=change.changetype(); %>
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+"的");%>
<% } %>