四则运算

package fo.dao;

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

import fo.domin.data;


public class foDao {
    public static void main(String[] args) {
//        data data=getData(9);
//        System.out.println(data.toString());
//        generate(90);
    }
    public static void generate(int number){

        Connection con=null;
        PreparedStatement psts=null;
     
     
        try {
            con=JDBCUtils.getConnection();
        
    for(int i=1;i<=number;i++){
            String sql="INSERT INTO fooperation VALUES(?,?,?,?,?)";
               psts=con.prepareStatement(sql);
            int random=(int)(Math.random()*4);
            String symbol="";
            int digital1=(int)(Math.random()*100);
            int digital2=(int)(Math.random()*100);
            int result=0;
        switch(random){
            case 0:
                result=digital1+digital2;
                while(result>100){
                    digital1=(int)(Math.random()*100);
                    digital2=(int)(Math.random()*100);
                    result=digital1+digital2;
                }
                  symbol="+";
            
                  break;
            case 1:
                  result=digital1-digital2;
                  while(digital1-digital2<0){
                   digital1=(int)(Math.random()*100);
                   digital2=(int)(Math.random()*100);
                   result=digital1-digital2;
                  }
                  symbol="-";
                
                  break;
            case 2:
                 result=digital1*digital2;
                  while(digital1*digital2>100){
                   digital1=(int)(Math.random()*100);
                   digital2=(int)(Math.random()*100);
                   result=digital1*digital2;
                  }
                  symbol="*";
                
                  break;
            case 3:
                  digital2=(int)(Math.random()*100)+1;
                  while(digital1%digital2!=0||digital1<digital2){
                   digital1=(int)(Math.random()*100);
                   digital2=(int)(Math.random()*100)+1;
                   result=digital1/digital2;
                  }
                  symbol="/";
                 
                  break;
            }
            psts.setInt(1, i);
            psts.setString(2, symbol);
            psts.setInt(3, digital1);
            psts.setInt(4, digital2);
            psts.setInt(5, result);
            psts.executeUpdate();
        }
        
    }catch(RuntimeException e){
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        finally{
             try{
                 if(psts!=null){
                     psts.close();
                 }
                 if(con!=null){
                     con.close();
                 }
                }
                 catch(SQLException e){
                     e.printStackTrace();
                     
                 }
        }
    
    
     }
    
    public static data getData(int number){
         Connection con=null;
             PreparedStatement psts=null;
             ResultSet rs=null;
          try{
           con=JDBCUtils.getConnection();
          String sql="SELECT * FROM fooperation WHERE number=?";
           psts=con.prepareStatement(sql);
          psts.setInt(1, number);
          rs=psts.executeQuery();
          if(rs==null){
              return null;
          }
          if(rs.next()){
              data datas=new data();
              datas.setNumber(number);
              datas.setSymbol(rs.getString("symbol"));
              datas.setDigital1(rs.getInt("digital1"));
              datas.setDigital2(rs.getInt("digital2"));
              datas.setResult(rs.getInt("result"));

             return datas;
          }
          else {
              return null;
          }
          }
          catch(Exception e){
             e.printStackTrace();
         }
         finally{
             try{
             if(psts!=null){
                 psts.close();
             }
             if(con!=null){
                 con.close();
             }
            }
             catch(SQLException e){
                 e.printStackTrace();
                 
             }
         }
        return null;
             
         }
        
        
    
}
package fo.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
        public static Properties props=null;
        static {
            try{
             InputStream in=JDBCUtils.class.getResourceAsStream("dbconfig.properties");
              props=new Properties();
             props.load(in);
            }
            catch(IOException e){
                throw new RuntimeException();
            }
             try {
                Class.forName(props.getProperty("driverClassName"));
            } catch (ClassNotFoundException e) {
                throw new RuntimeException();
            }
        }
          public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException{
             
              return DriverManager.getConnection(props.getProperty("url"),props.getProperty("username"),props.getProperty("password"));
          }
        
}
package fo.domin;

public class data {
       private int number;
       private String symbol;
       private int digital1;
       private int  digital2;
       private int result;
    public data() {
        super();
        // TODO Auto-generated constructor stub
    }
    public data(int number, String symbol, int digital1, int digital2, int result) {
        super();
        this.number = number;
        this.symbol = symbol;
        this.digital1 = digital1;
        this.digital2 = digital2;
        this.result = result;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
    public int getDigital1() {
        return digital1;
    }
    public void setDigital1(int digital1) {
        this.digital1 = digital1;
    }
    public int getDigital2() {
        return digital2;
    }
    public void setDigital2(int digital2) {
        this.digital2 = digital2;
    }
    public int getResult() {
        return result;
    }
    public void setResult(int result) {
        this.result = result;
    }
    @Override
    public String toString() {
        return "data [number=" + number + ", symbol=" + symbol + ", digital1=" + digital1 + ", digital2=" + digital2
                + ", result=" + result + "]";
    }
    
    
       
}
package fo.service;

import fo.dao.foDao;
import fo.domin.data;

public class foService {
    private foDao userdao=new foDao();
    public void generate(int number){
        userdao.generate(number);
    }
    public data getData(int number){
        data data=userdao.getData(number);
        return data;
    }
    public boolean judge(data form){
        data data=userdao.getData(form.getNumber());
        int correctResult=data.getResult();
        int formResult=form.getResult();
        if(correctResult==formResult){
            return true;
        }
        else return false;
    }
}
package fo.service;

public class myException extends Exception {

    public myException() {
        super();
        // TODO Auto-generated constructor stub
    }

    public myException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        // TODO Auto-generated constructor stub
    }

    public myException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

    public myException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public myException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }
  
}
package fo.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fo.domin.data;
import fo.service.foService;

public class displayServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        foService foservice=new foService();
        String num=(String)request.getSession().getAttribute("number");
        int number=Integer.parseInt(num);
        for(int i=1;i<=number;i++){
        data form=new data();
        form.setResult(Integer.parseInt((String)request.getParameter("result"+i)));
        form.setNumber(i);
        request.getSession().setAttribute("form"+i, form);
        boolean resultMasg=foservice.judge(form);
        if(resultMasg){request.getSession().setAttribute("masg"+i, "正确");}
        else {request.getSession().setAttribute("masg"+i, "错误");}
        }
        request.getRequestDispatcher("/resultPage.jsp").forward(request, response);
        
    }

}
package fo.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fo.domin.data;
import fo.service.foService;

public class enterServlet extends HttpServlet {


    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          foService foservice=new foService();
          String number=request.getParameter("number");
          int num=Integer.parseInt(number);
          request.getSession().setAttribute("number", number);
          foservice.generate(num);
          for(int i=1;i<=num;i++){
              data data= foservice.getData(i);
              request.getSession().setAttribute("data"+i, data);
          }
          request.getRequestDispatcher("/display.jsp").forward(request, response);
    }

}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.sql.*" %>
<%@ page import="fo.domin.*" %>
<!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>Insert title here</title>
</head>
<body>
<form action="<c:url value='displayServlet'/>" method="post">
<%
String number=(String)session.getAttribute("number");
int num=Integer.parseInt(number);
for(int i=1;i<=num;i++){
    data data=new data();
    data=(data)session.getAttribute("data"+i);
    out.print(data.getDigital1()+data.getSymbol()+data.getDigital2()+"=");
%>
<input type="text" οnkeyup="this.value=this.value.replace(/\D/g,'')"
   name="result<%=i%>"/><br/>
<%} %>
<input type="submit" value="提交"/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Insert title here</title>
</head>
<body>
   <form  action="<c:url value='enterServlet'/>" method="post">
     请输入题目数量:<input onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" name="number"/>
     <input type="submit" value="提交"/>

   </form>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.sql.*" %>
<%@ page import="fo.domin.*" %>
<!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>Insert title here</title>
</head>
<body>
<%
String number=(String)session.getAttribute("number");
int num=Integer.parseInt(number);
for(int i=1;i<=num;i++){
    data data=new data(1,"+",2,3,5);
    data=(data)session.getAttribute("data"+i);
    data form=new data(1,"+",2,3,5);
    form=(data)session.getAttribute("form"+i);
    String correctResult=(String)session.getAttribute("masg"+i);
    out.println(data.getDigital1()+data.getSymbol()+data.getDigital2()+"="+form.getResult()+" 答案"+correctResult);
    %>
    <br/>
<%}
%>


</body>
</html>

 

posted @ 2019-11-08 19:02  派大星球日记  阅读(213)  评论(0编辑  收藏  举报