JAVA-WEB-简单的四则运算

Posted yizhixiaozhu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA-WEB-简单的四则运算相关的知识,希望对你有一定的参考价值。

首先附上选择题目数量和每行题数的JSP代码

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<meta http-equiv="Content-Type" content="text/html;charset=ytf-8">
<title>四则运算</title>
</head>
<%
    Object message=request.getAttribute("message");
    if(!(message==null||message.equals("")))
    {
        %>
        <script type="text/javascript">alert(\'<%=message%>\')</script>
        <%
    }
%>
<body style="background-color:pink" align="center">
    <form action="SizeServlet?method=submit" method="post">
    <br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
        <p style="padding-left:200px;">请输入您要出多少道题:<input type="text" style="width:50px" name="sum"></p>
        <p style="padding-left:200px;">请输入一行几到题:<input type="text" style="width:50px" name="n"></p>
        <p style="padding-left:200px;"><input type="submit"  value="提交"></p>
    </form>

</body>
</html>

 

 

 

现在是接收的Servlet层

package math;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SizeServlet
 */
@WebServlet("/SizeServlet")
public class SizeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SizeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException
    {
        arg0.setCharacterEncoding("utf-8");
        arg1.setCharacterEncoding("utf-8");
        String function=arg0.getParameter("method");
        if(function.equals("submit"))
        {
            String str;
            int sum,n;
            str=arg0.getParameter("sum");
            if(str==""||str==null)
            {
                arg0.setAttribute("message", "提交失败,题目数不能为空");
                arg0.getRequestDispatcher("NewFile1.jsp").forward(arg0, arg1);
                return;
            }
            sum=Integer.valueOf(str).intValue();
            str=arg0.getParameter("n");
            if(str==""||str==null)
            {
                arg0.setAttribute("message", "提交失败,每行个数不能为空");
                arg0.getRequestDispatcher("NewFile1.jsp").forward(arg0, arg1);
                return;
            }
            n=Integer.valueOf(str).intValue();
            SizeService.setSum(sum);
            SizeService.setN(n);
            if(sum<=1000&&n<=8&&n>0)
            {
                arg0.setAttribute("message", "提交成功");
                arg0.getRequestDispatcher("NewFile.jsp").forward(arg0, arg1);
                return;
            }
            else
            {
                arg0.setAttribute("message", "提交失败");
                arg0.getRequestDispatcher("NewFile1.jsp").forward(arg0, arg1);
                return;
            }
        }
    }
    
    
    
    
    

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

然后是出题的代码

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ page language="java" import="math.StringHandle"%>
<%@ page language="java" import="math.EntityToString"%>
<%@ page language="java" import="math.MathGenerate"%>
<%@ page language="java" import="math.SizeService"%>
<title>四则运算</title>
</head>

<body style="background-color:pink" align="center">
<form action="NewFile2.jsp">
    <table align="center">
    
<%
    MathGenerate mg=new MathGenerate();
    int m,n;
    m=SizeService.getSum();
    n=SizeService.getN();
    mg.generate(m, 1);
    for(int i=0;i<mg.getAnwser().size();)
    {
        %>
        <!-- 一行有几个 -->
        <tr>
        <% 
        for(int j=0;j<n&&i<m;j++,i++)
        {
            
        
    %>
    
    <td ><%=mg.getQuestion().get(i) %></td>
    <td>=</td>
    <td><input type="hidden" name="question" value=<%=mg.getQuestion().get(i) %>></td>
    <td><input type="text" style="width:50px" name="answer" ></td>
    <td><input type="hidden" name="daan" value=<%=mg.getAnwser().get(i) %>></td>
    
    <%} %>
    </tr> 
  <%}%>
 
  
</table>
<p align="center"><input type="submit" value="提交"></p>
</form>
</body>
</html>

 

其中出题代码中的MathGenerate()是自己定义的一个出题的类,专门用来出题,这里可以改成自己写的任何其他的出题类

 

 

 

然后就是判断做题是否正确的代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<%@ page language="java" import="math.SizeService"%>
<title>四则运算</title>
</head>

<body style="background-color:pink" align="center">


<table align="center">
<%
    
    
    String answer[]=request.getParameterValues("answer");
    String daan[]=request.getParameterValues("daan");
    String question[]=request.getParameterValues("question");
    int m,n;
    m=SizeService.getSum();
    n=SizeService.getN();

    for(int i=0;i<m;i++)
    {
        if(answer[i].equals(""))
        {
        %>
        <tr>
        <td>您没有做第<%=i+1 %>题:</td>
        <td><%=question[i] %></td>
        <td>=</td>
        <td>&nbsp;</td>
        <td>这道题的正确答案是<%=daan[i] %></td>
        </tr>
        <%}
        else if(daan[i].equals(answer[i]))
        {
        %>
        <tr>
        <td>您做的第<%=i+1 %>题:</td>
        <td><%=question[i] %></td>
        <td>=</td>
        <td><%=answer[i] %></td>
        <td>恭喜您做对了这道题</td>
        </tr>
        <%
        }
        else
        {    
        %>
        <tr>
        <td>您做的第<%=i+1 %>题:</td>
        <td><%=question[i] %></td>
        <td>=</td>
        <td><%=answer[i] %></td>
        <td>很遗憾您做错了这道题,正确答案是<%=daan[i] %></td>
        </tr>
        <%} %>
        
  <%}%>
</table>
    <a href="NewFile1.jsp"><button style="border:0px solid;background:lightblue;margin-left:50%;">重新做题</button></a>
    <br>
    <br>
    <a href="NewFile3.jsp"><button style="border:0px solid;background:red;margin-left:50%;">结束答题</button></a>
    
</body>
</html>

 

 

这个主要的就是值的传递

然后就是最简单的结束代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>结束</title>
</head>
<body  style="background-image:url(44444.jpg);" align="center">
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<h1 style="color:red;font-size:500%"  align="center">结束答题!</h1>
</body>
</html>

其中插了一张图片可根据自己需要删除

接下来是运行过程图片

 

 

 

提交之后进入答题界面:

 

 

点击提交进入判断正误界面(我没做这个题所以是这个样子)

 

 

在判断完正误之后可以选择继续答题或者结束答题

 

 

 

 

 结束答题时这个样子的

 

 

 

 

这个四则运算主要的就是在JSP文件中值进行传递和页面的跳转其他的问题都时一些JAVA封装好的类然后进行调用

以上是关于JAVA-WEB-简单的四则运算的主要内容,如果未能解决你的问题,请参考以下文章

java-web——第六课 Servlet 控制器的引入

Java-web实现用户登录注册功能

java-web中生成文档

java-web项目之jsp脚本访问mysql数据库

java项目和java-web项目中文件和文件夹的含义

java-web环境搭建