软件工程个人作业——四则运算

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    主页
    
    
        
    
    
    
  
  
  
    

首页
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    做题页
    
    
    
        
    
    
    

  
  
  
   
<% dao.DBUtil db=new dao.DBUtil(); Connection conn =db.getConn(); Statement stmt = conn.createStatement();//从数据库里面取东西对比 ResultSet rs=stmt.executeQuery("select * from yunsuan"); int x=0; while(rs.next()) { x++; int num1=rs.getInt("num1"); int num2=rs.getInt("num2"); String symbol=rs.getString("symbol"); %>

<% } %>

出题页
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    答案页
    
    
    
        
    
    
    

  
  
  
  <% 
          dao.DBUtil db=new dao.DBUtil();
          Connection conn =db.getConn();
        Statement stmt = conn.createStatement();//从数据库里面取东西对比    
        ResultSet rs=stmt.executeQuery("select * from yunsuan");
        int x=0;
        while(rs.next())
        {
            x++;
            String y=String.valueOf(x);
            int num1=rs.getInt("num1");
            int num2=rs.getInt("num2");
            String symbol=rs.getString("symbol");
            int find=rs.getInt("find");
            if(request.getParameter(y)!=null)
            {
                int a=Integer.parseInt(request.getParameter(y));
   %>
   

<%=a %>
<% if(find==a) { %> 回答正确 <% } else { %> 回答错误,正确答案为<%=find %> <% } %>

<% } else { out.print("出错"); } } %>

答案页
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Random;
import java.util.Scanner;

import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.Dao;
import user.users;

public class YunServlet extends HttpServlet {
    /**
         * Constructor of the object.
         */
    public YunServlet() {
        super();
    }

    /**
         * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * *
@param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * *
@param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { dao.DBUtil db=new dao.DBUtil(); Connection conn =db.getConn(); Dao open=new Dao(); int num=Integer.parseInt(request.getParameter("num")); //HttpSession session=request.getSession(); //session.setAttribute("num", num); Random r=new Random(); for(int i=0;i) { users u=new users(); int r1=r.nextInt(100)+1; int r2=r.nextInt(100)+1; int r3=r.nextInt(4)+1; switch(r3) { case 1: { int x=r1+r2; while(x>100&&x>0) { r1=r.nextInt(100)+1; r2=r.nextInt(100)+1; x=r1+r2; } u.num1=r1; u.num2=r2; u.symbol="+"; u.find=x; try { open.add(conn, u); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } case 2: { int x=r1-r2; while(x>100&&x>0) { r1=r.nextInt(100)+1; r2=r.nextInt(100)+1; x=r1-r2; } u.num1=r1; u.num2=r2; u.symbol="-"; u.find=x; try { open.add(conn, u); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } case 3: { int x=r1*r2; while(x>100&&x>0) { r1=r.nextInt(100)+1; r2=r.nextInt(100)+1; x=r1*r2; } u.num1=r1; u.num2=r2; u.symbol="*"; u.find=x; try { open.add(conn, u); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } case 4: { int x=r1/r2; int root=r1%r2; while(x>100&&root==0&&x>0) { r1=r.nextInt(100)+1; r2=r.nextInt(100)+1; x=r1/r2; root=r1%r2; } u.num1=r1; u.num2=r2; u.symbol="/"; u.find=x; try { open.add(conn, u); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } } request.getRequestDispatcher("../MyJsp.jsp").forward(request,response); } /** * Initialization of the servlet.
* *
@throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
数据写入

以上是四则运算的源码

设计思想:

通过网页输入做题数,和时间,提交到下一个界面。servlet类生成数据存入数据库。做题页提交后到下一个页面显示进行判断对错。

你可能感兴趣的:(java,数据库)