• 四则运算2.0总结


    设计思想:

    创建两个.java文件:

    Arithmetic.java:

    生成四则运算表达式并储存在ArrayList的对象中。

    Result.java:

    根据传进来的输入值计算结果,统计正确的个数,错误的个数,未做的个数。

    三个.jsp文件:

    Welcome.jsp:

    欢迎界面,设置出题数,答题时间。跳转到operationResult.jsp界面

    showExpression.jsp:

    显示四则运算表达式,并输入结果,提交后跳转到operationResult.jsp界面

    operationResult.jsp:

    根据输入的答案显示出统计的信息。

    源程序代码:

    Arithmetic.java:

    import java.util.ArrayList;
    
    public class Arithmetic 
    {
        private static int []result;
        private static ArrayList<String> expressions;
        private static int numbers;
        private int x;
        private int y;
        public void setExpression(int numbers)//返回表达式
        {
            this.numbers=numbers;
            String operator;
            
            result=new int[numbers];
            expressions=new ArrayList<String>();
            String expression=null;
            for(int i=0;i<numbers;i++)
            {
            int random=(int)(Math.random() * 4 + 1);
            x=(int)(Math.random() * 100 + 1);
            y=(int)(Math.random() * 100 + 1);
            switch(random)
            {
            case 1:{
                operator="+";
                optimizeResult(x,y,operator);
                result[i]=x+y;
                expression=x+operator+y+"=";
                expressions.add(expression);
                }break;
            
            case 2:{
                operator="-";
                optimizeResult(x,y,operator);
            result[i]=x-y;
            expression=x+operator+y+"=";
            expressions.add(expression);
            }break;
            
            case 3:{
                operator="*";
                optimizeResult(x,y,operator);
                result[i]=x*y;
            expression=x+operator+y+"=";
            expressions.add(expression);
            }break;
            
            case 4:{
                operator="/";
                optimizeResult(x,y,operator);
            optimizeDivision(x ,y);
            result[i]=x/y;
            expression=x+operator+y+"=";
            expressions.add(expression);
            }break;
            }
            }
        }
        public  ArrayList<String> getExpression()
        {
            return expressions;
        }
        public int[] getResult()
        {
            return result;
        }
        public void optimizeDivision(int a,int b)
        {
            while(a%b!=0)
            {
                a=(int)(Math.random() * 100 + 1);
                b=(int)(Math.random() * 100 + 1);
            }
            
            x=a;
            y=b;
        }
        public void optimizeResult(int a,int b,String c)
        {
            int result=101;
            
            for(;result>100;){
                a=(int)(Math.random() * 100 + 1);
                b=(int)(Math.random() * 100 + 1);
            switch(c)
            {
            case "+":result=a+b;break;
            case "-":result=a-b;break;
            case "*":result=a*b;break;
            case "/":result=a/b;break;
            }
    
            }
            x=a;
            y=b;
            
        }
        public int getNumbers()
        {
            return numbers;
        }
        public void reset()
        {
            numbers=0;
            result=null;
            expressions=null;
        }
        public boolean judgeErrorInput(String number)
        {
            boolean flag=false;
            if(number==null||"".equals(number))
            {
                flag=true;
                return flag;
            }
            for(int i=0;i<number.length();i++)
            {
                if(!(number.charAt(i)>=48&&number.charAt(i)<=57))
                {
                    flag=true;
                    break;
                }
            }
            return flag;
        }
    }

    Result.java:

    import java.util.ArrayList;
    
    public class Result
    {
        static ArrayList<String>  resultSExpression=null;
        static ArrayList<String>  resultSAnswer=null;
        String resultExpression;
        String resultAnswer;
        static int doRightNumbers=0;
        static int doWrongNumbers=0;
        static int unCompletedNumbers=0;
        public Result()
        {
            resultSExpression=new ArrayList<String>();
            resultSAnswer=new ArrayList<String>();
            
        }
       public void addResult(String expression,int rightResult,int inputResult)
       {
           if(rightResult==inputResult)
           {
               resultExpression=expression+inputResult;
               resultAnswer="  正确";
               doRightNumbers++;
           }
           else if(inputResult==-9999)
           {
               resultExpression=expression+"  ? ";
               resultAnswer="未做    (答案为"+rightResult+")";
               unCompletedNumbers++;
           }
           else
           {
               resultExpression=expression+inputResult;
               resultAnswer="  错误    (答案为"+rightResult+")";
               doWrongNumbers++;
           }
           resultSExpression.add(resultExpression);
           resultSAnswer.add(resultAnswer);
       }
       public ArrayList<String> getResultSExpression()
       {
           return resultSExpression;
       }
       public ArrayList<String> getResultSAnswer()
       {
           return resultSAnswer;
       }
       public int getDoRightNumbers()
       {
           return doRightNumbers;
       }
       public int doWrongNumbers()
       {
           return doWrongNumbers;
       }
       public int getUnCompletedNumbers()
       {
           return unCompletedNumbers;
       }
       public void reset()
       {
           doRightNumbers=0;
           doWrongNumbers=0;
           unCompletedNumbers=0;
           
       }
    }

    Welcome.jsp:

    <%@ 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 background="C:Userswin8workspace四则运算Arithmeticimaged602adc701a429f16a32aac5579f113e_7205982_162234868593_2.jpg">
    <center>
    <font Face="楷体" color="white" size="6">欢迎进入四则运算小练习</font>
    </center>
    <form name="welcome" action="showExpression.jsp" method="post">
    <br><br><br>         
    <center style="color:white;font-family:楷体; font-size:20px"> 设置出题的数目 
                    <input type="text" name="exercisesNumber" class="inputField">
               <input type="submit" value="出题" style="color:green">
     </center>
    </form>
    
    </body>
    </html>

    showExpression.jsp:

    <%@ page import ="java.util.ArrayList" 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 background="C:Userswin8workspace四则运算Arithmeticimaged602adc701a429f16a32aac5579f113e_7205982_162234868593_2.jpg">
    <jsp:useBean id="arithmetic" class="util.Arithmetic" scope="application" />
    <%
        String exercisesNumber=(String)request.getParameter("exercisesNumber");
        int numbers=0;
        int i=0;
        ArrayList<String> expressionStorage=null;
        if(arithmetic.judgeErrorInput(exercisesNumber))
        {
            out.print("<script language='javaScript'> alert('输入错误');</script>");
            response.setHeader("refresh", "0;url=welcome.jsp");
        }
        else 
        {
             numbers=Integer.parseInt(request.getParameter("exercisesNumber"));
             if(numbers<=0)
             {
                 out.print("<script language='javaScript'> alert('输入的题目数量不能为零');</script>");
                 response.setHeader("refresh", "0;url=welcome.jsp");
             }    
             else
                {
                     numbers=Integer.parseInt(request.getParameter("exercisesNumber"));
                     arithmetic.setExpression(numbers);
                        expressionStorage=arithmetic.getExpression();
                     String result="";
     
    %>
    <form name="" action="operationResult.jsp" method="post">
    <table border="0">
    <%for(String expression: expressionStorage) {%>
       <tr> 
           <td style="color:white"><%=expression%> </td>
          
           <td><input type="text" placeholder="请输入答案" name=<%="result"+i++ %> ></td>
      </tr>
                <%
                }
                %>
            
    </table>
    
                    <input type="submit" value="提交" style="color:green">
    </form>
    
    </body>
        <%} %>
    <%} %>
    </html>

    operationResult.jsp:

    <%@page import="java.util.Enumeration"%>
    <%@page import="java.util.ArrayList"%>
    <%@page import="util.Result"%>
    <%@ 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 background="C:Userswin8workspace四则运算Arithmeticimaged602adc701a429f16a32aac5579f113e_7205982_162234868593_2.jpg">
    <jsp:useBean id="arithmetic" class="util.Arithmetic" scope="page" />
    <%
    
    Result result=new Result();
    int []receiveResult=new int[arithmetic.getNumbers()];
    int []rightReuslt=arithmetic.getResult();
    
    Enumeration<String> names=request.getParameterNames();
    String input=null;
       for(int i=0;names.hasMoreElements();i++){
           input=request.getParameter(names.nextElement());
           if(input==null||"".equals(input))
               receiveResult[i]=-9999;
           else
               receiveResult[i]= Integer.parseInt(input);
              
       }
    ArrayList<String> expressionStorage=arithmetic.getExpression();
    for(int i=0;i<arithmetic.getNumbers();i++)
    {
        result.addResult(expressionStorage.get(i),rightReuslt[i],receiveResult[i]);
    }
    ArrayList<String> outResultExpression=result.getResultSExpression();
    ArrayList<String> outResultAnswer=result.getResultSAnswer();
           %>
    </body>
    
    
    结果:
    <table>
    <%for(int i=0;i<arithmetic.getNumbers();i++){ %>
    <tr>
    
        <td><%= outResultExpression.get(i) %></td>
        <td><%= outResultAnswer.get(i) %></td>
    
    </tr>
    
    <%} %>
    </table>
    做对的个数为:<%= result.getDoRightNumbers()%>
     <br>
    做错的个数为:<%= result.doWrongNumbers()%>
     <br>
     未完成的个数:<%= result.getUnCompletedNumbers() %>
    <% 
    result.reset();
    arithmetic.reset();
    %>
    <form action="welcome.jsp">
    
     <input type="submit" value="再次答题" style="color:green">
     
    </form>
    
    </html>

    运行结果截图:

    开始界面

    什么都不输入

     

    输入负数

     

     

    输入零

     

    正常输入

     

    编程结果总结分析:

    1.一定要设计好程序的大概思路,这样写起来就是完成一个一个具体的功能模块了。

    2.还要想清楚模块之间的衔接,如何传递想要的值。

    3.把框架写好了,如果没写注释的话,要在主要地方补上注释,这样放几天后在拿起了完善功能时就很方便。

    4.优化程序前最好要把原代码先粘在一个文本中,而我本次在完善非法输入判断时就直接删了,后来有一个小问题没注意到,一直报错,浪费了很多时间。

    5.多看书,多看一些资料,就会发现其实有很多已经存在的可以直接使用的方法,这样会节省很多时间。

    6.把所有的功能模块写在.java中,在jsp只调用那些方法即可。

                   时间记录日志:

                    学生:XXX                                      日期:2017.11.22

                    教师:XXX                                      课程:XXX

    日期

    开始时间

    结束时间

    中断时间

    净时间

    活动

    备注

    C

    U

    11.22

    19:30

     

    21:30

    15

    95

    完成框架四则运算框架

    作业

     

     

    11.23

    15:30

    17:00

    20

    70

    完善程序,可以输入答案并判断对错

    作业

     

     

    11.26

    15:00

    16:30

    0

    90

    完善程序,可以统计判断对错及未输入的个数

    作业

     

     

    11.29

    19:00

    20:30

    10

    80

    完善程序,优化输入答案界面及显示结果界面,并添加开始界面

    作业

     

     

     

  • 相关阅读:
    云小课|MRS基础原理之Oozie任务调度
    Java:既然有了synchronized,为什么还要提供Lock?
    云小课|MRS基础原理之CarbonData入门
    Workflow,要不要了解一下
    软件要想做的好,测试必定少不了
    什么是ForkJoin?看这一篇就能掌握!
    手把手教你从安装CentOS7.4镜像开始,搭建IoT视频监控系统
    案例解读华为隐私计算产品TICS如何实现城市跨部门数据隐私计算
    13个QA带你了解线上压测的知识点
    云小课|MRS基础原理之Hudi介绍
  • 原文地址:https://www.cnblogs.com/ssyh/p/7993265.html
Copyright © 2020-2023  润新知