四则运算2

Posted peanut

tags:

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

1.设计思路

用javaweb以及java共同设计一个四则运算,页面为web,主题为java

2.源代码

 

package dao;
import java.util.*;
public class YunSuan 
{
    static int value=0;
    public YunSuan()
    {
        
    }
    public void setValue()
    {
        value++;
    }
    public int getValue()
    {
        return value;
    }
    Random rd=new Random();
    String fh[]={"+","-","*","/"};
    int num1;
    int num2;
    int h;
    static int t,f;
    
    public int getT() {
        return t;
    }
    public  void setT() {
        t++;
    }
    public int getF() {
        return f;
    }
    public void setF() {
        f++;
    }
    int number;
    static int answer;
    public int getAnswer() {
        return answer;
    }
    public void setAnswer(int answer) {
        this.answer = answer;
    }
    public int getNum1() {
        
        return num1;
    }
    public void setNum1(int num1) {
        this.num1 =num1;
    }
    public int getNum2() {
        
        return num2;
    }
    public void setNum2(int num2) {
        this.num2 =num2;
    }
    public int getH() {
        
        return h;
    }
    public void setH(int h) {
        this.h = h;
    }
    public String[] getFh() {
        return fh;
    }
    public void fuzhi()
    {
        Random rd=new Random();
        num1=1+rd.nextInt(101);
        num2=1+rd.nextInt(101);

         h=rd.nextInt(4);
        setNum1(num1);
        setNum2(num2);
        setH(h);
        
        if(fh[h].equals("+"))
        {
            if(num1+num2<=100)
            {
                setAnswer(num1+num2);
            }
            else
            {
                fuzhi();
            }
            
        }
        else if(fh[h].equals("-"))
        {
            setAnswer(num1-num2);
        }
        else if(fh[h].equals("*"))
        {
            if(num1*num2<=0)
            {
                setAnswer(num1*num2);
            }
            else
            {
                fuzhi2();
            }

        }
        else if(fh[h].equals("/"))
        {
            if(getNum1()%getNum2()==0)
            {
            setAnswer(num1/num2);
            }
            else
            {
                
                fuzhi();
            }

        }
        
    }
    
    public void fuzhi2()
    {
        Random rd=new Random();
        num1=1+rd.nextInt(11);
        num2=1+rd.nextInt(11);
        
        setNum1(num1);
        setNum2(num2);
        
        setAnswer(num1*num2);
    }
     public void showt()
     {
         
     }
    
     public static void main(String args[])
    {
        YunSuan ys=new YunSuan();
        ys.fuzhi();
        System.out.println(ys.getNum1()+ys.getFh()[ys.getH()]+ys.getNum2()+"="+ys.getAnswer());
    }
}
package server;

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 YunSuan
 */
@WebServlet("/YunSuan")
public class YunSuan extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public YunSuan() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @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);
    }

}
package util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class ValidateUtil {
    static int i=0;
    public static  boolean validateNull(HttpServletRequest request,String[] fileds) {
        boolean validate = true;
        //map对象用来装载不同的错误信息
        Map<String,String> errorMsg = new  HashMap();
        for(String filed :fileds) {
            String value = request.getParameter(filed);
            if (value == null || "".equals(value.trim())) {
                validate = false;
                i++;
                errorMsg.put(filed, "目前未答"+i+"道!");
            }
            if (!validate) {
                request.setAttribute("errormsg", errorMsg);
            }
            
        }
        
        return validate;
    }
    public static String showError(HttpServletRequest request , String filed) {
        Map<String, String> errorMsg = (Map<String,String>)request.getAttribute("errormsg");
        if (errorMsg == null) {
            return "";
        }
        String msg = errorMsg.get(filed);
        if (msg == null) {
            return "";
        }
        return msg;
    }

}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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  style="background-image:url(\'../image/2.jpg\');">
<center style="color:red ; font-family:华文行楷;font-size:80px">谢谢使用!</center>

</body>
</html>
<%@page import="dao.YunSuan"%>
<%@page import="java.util.*" %>
<%@page import="util.ValidateUtil"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>四则运算</title>
</head>


<body  style="background-image:url(\'../image/2.jpg\');">
<%Map<String,String> errorMsg = (Map<String,String>)request.getAttribute("errormsg"); %>

<form action="page2.jsp" method="get">
<center style="color:red ; font-family:华文行楷;font-size:35px">四则运算</center>
<br>
<br>
<%

YunSuan ys=new YunSuan();
ys.fuzhi();
%>
<center  style="color:black ; font-family:楷体;font-size:20px"> 第<%=ys.getValue()+1%>题:<%
             ys.setValue();
             out.println(ys.getNum1()+ys.getFh()[ys.getH()]+ys.getNum2());
             

%></center>
<!--  <script language=\'javascript\' > setTimeout("alert(\'5 seconds!\')",5000);</script>-->
<br>
<br>

<center style="color:black ; font-family:楷体;font-size:15px" > 输入答案 <input  type="text"  name="answer" placeholder="请输入答案"/></center>
<br>
<center style="color:red ; font-family:楷体;font-size:20px"><%
out.println("目前正确"+ys.getT()+"道!");
%></center>
<br>
<center style="color:red ; font-family:楷体;font-size:20px"><%
out.println("目前错误"+ys.getF()+"道!");
%></center>
<br>

<center style="color:red ; font-family:楷体;font-size:20px"><%=ValidateUtil.showError(request, "answer") %></center>
<br>

<center><input type="submit" value="提交" /></center>
<br>
<center><a href="end.jsp"><input type="button" value="退出" /></a></center>


</form>
</body>
</html>
<%@page import="dao.YunSuan"%>
<%@page import="util.ValidateUtil"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body  style="background-image:url(\'../image/2.jpg\');">
<jsp:useBean id="yy" class="dao.YunSuan" scope="page"/>
<%
String an=request.getParameter("answer");
boolean validate = ValidateUtil.validateNull(request, new String[]{"answer"});
if(!validate){
    %>
    <jsp:forward page="page.jsp"></jsp:forward>
    <%
        }
int answer=Integer.parseInt(an);
    if(answer==yy.getAnswer())
{
        yy.setT();
        
%>
<br>
<script language=\'javaScript\' > alert(\'正确\');</script>

<%
response.setHeader("refresh", "0;url=page.jsp");
}
else
{
    yy.setF();
%>

<script language=\'javaScript\'> alert(\'错误\');</script>
<%
response.setHeader("refresh", "0;url=page.jsp");
}
%>    

</body>
</html>

3.运行截图

4.时间记录日志

Personal Software Process Stages Time
 需求分析 (包括学习新技术) 0.5
生成设计文档 0.5
设计复审 0.5
代码规范 0.5
具体设计 1
具体编码 2
代码复审 1
测试(自我测试,修改代码,提交修改) 3
合计 9

 

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

Java基础知识 自增自减运算符

如何在 python 中并行化以下代码片段?

20个简洁的 JS 代码片段

获取 badarith,[erlang,'+',[error,0],[],同时使用 Erlang 片段在 TSUNG 中执行算术运算

20194626 自动生成四则运算题第一版报告

20个简洁的 JS 代码片段