• 软件工程结对作业01(四则运算网页版)


    程序设计思想:

    首先利用随机数产生器生成1至5之间的随机整数,但产生的随机数为5时,则让其产生3个100以内的随机整数,否则产生2个随机整数运算操作数,在通过判断控制产生的结果

    在100以内的式子.之后再写主界面,出题界面与结果界面.

      1 package com.jaovo.msg.model;
      2 
      3     public class arithmetic
      4     {    public int []answer;//答案
      5         public int shumu;//出题数目
      6         public String []suanshi;//算式
      7         public int[] getAnswer() 
      8         {
      9             return answer;
     10         }
     11 
     12         public void setAnswer(int[] answer) 
     13         {
     14             this.answer = answer;
     15         }
     16 
     17         public int getShumu()
     18         {
     19             return shumu;
     20         }
     21 
     22         public void setShumu(int shumu) 
     23         {
     24             this.shumu = shumu;
     25         }
     26 
     27         public String[] getSuanshi() 
     28         {
     29             return suanshi;
     30         }
     31 
     32         public void setSuanshi(String[] suanshi) 
     33         {
     34             this.suanshi = suanshi;
     35         }
     36 
     37         public void setsuanshi(String[] suanshi) 
     38         {
     39             this.suanshi = suanshi;
     40         }
     41         
     42         
     43     
     44     public String [] biaodashi(int n)
     45     {
     46         
     47         shumu=n;
     48         answer=new int[n];
     49         int a=0;
     50         int b=0;
     51         int c=0;
     52         int d1 = 0;
     53         int d=0;
     54         int d2=0;
     55         int []mixture=new int[2];//运算符数组
     56         String []biaodashi=new String[n];
     57 
     58         for(int i=0;i<n;i++)
     59         {
     60         a=(int)(Math.random()*100)+1;//1-100
     61         b=(int)(Math.random()*100)+1;
     62         c=(int)(Math.random()*5)+1;//随机生成一个1-5的整数,4表示加法,1表示减法,2表示乘法,3表示除法,5表示混合
     63     if(c==5)//混合运算
     64     {
     65         do
     66         {    
     67             for(int m=0;m<2;m++)
     68             {
     69                 mixture[m]=(int)(Math.random()*2);//0-1
     70             }//控制运算符个数
     71 
     72     a=(int)(Math.random()*100)+1;
     73     b=(int)(Math.random()*100)+1;
     74     d=(int)(Math.random()*100)+1;//生成三个数
     75     if(mixture[0]==0&&mixture[1]==0)
     76         {
     77             biaodashi[i]=a+"+"+b+"+"+d+" = ";
     78             d1=a+b+d;
     79         }
     80     if(mixture[0]==1&&mixture[1]==1)
     81         {
     82             biaodashi[i]=a+"-"+b+"-"+d+" = ";
     83             
     84             d1=a-b-d;
     85         }
     86     if(mixture[0]==0&&mixture[1]==1)
     87         {
     88             biaodashi[i]=a+"+"+b+"-"+d+" = ";
     89             d1=a+b-d;
     90         }
     91     if(mixture[0]==1&&mixture[1]==0)
     92         {
     93             biaodashi[i]=a+"-"+b+"+"+d+" = ";
     94             
     95             d1=a-b+d;
     96         }
     97             } while(d1<0);
     98     answer[i]=d1;
     99     }
    100     if(c==4)//单加法
    101         {
    102         d1=a+b;
    103         biaodashi[i]=a+"+"+b+" = ";
    104         while(d1>100)
    105         {
    106         a=(int)(Math.random()*100)+1;
    107         b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99
    108         d1=a+b;
    109         }
    110         biaodashi[i]=a+"+"+b+" = ";
    111         answer[i]=d1;
    112     
    113         }
    114     if(c==1)//单减法
    115         {
    116         d1=a-b;
    117         while(d1<0)
    118         {
    119         a=(int)(Math.random()*100)+1;
    120         b=(int)(Math.random()*100)+1;
    121         d1=a-b;
    122         }
    123         biaodashi[i]=a+"-"+b+" = ";
    124         answer[i]=d1;
    125         
    126         }
    127     if(c==2)//乘法
    128         {
    129         a=(int)(Math.random()*10);//0-9
    130         b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99
    131         d1=a*b;
    132         while(a<1||b<1||d1>81)
    133         {
    134         a=(int)(Math.random()*10);//0-9
    135         b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99
    136         }
    137         d1=a*b;
    138         biaodashi[i]=a+"*"+b+" = ";
    139         answer[i]=d1;
    140         
    141         }
    142     if(c==3)//除法
    143         {
    144         d1=a/b;
    145         while(a%b!=0||a/b>9||(a<=81&&b>=10)||(a>9&&a==b)||(a>81))
    146         {
    147         a=(int)(Math.random()*100)+1;
    148         b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99
    149         }
    150         d1=a/b;
    151         biaodashi[i]=a+"÷"+b+" = ";
    152         answer[i]=d1;
    153         
    154         }
    155 
    156 //查重
    157     for(int k=i-1;k>=0;k--)
    158         {
    159         while(biaodashi[i].equals(biaodashi[k]))
    160         {
    161         a=(int)(Math.random()*100)+1;//1-100
    162         b=(int)(Math.random()*100)+1;
    163         c=(int)(Math.random()*5)+1;//随机生成一个1-5的整数,4表示加法,1表示减法,2表示乘法,3表示除法,5表示混合
    164         if(c==5)
    165         {
    166         do//混合运算
    167         {    
    168             for(int m=0;m<2;m++)
    169             {
    170             mixture[m]=(int)(Math.random()*2);//0-1
    171             }//控制运算符
    172         
    173         a=(int)(Math.random()*100)+1;
    174         b=(int)(Math.random()*100)+1;
    175         d=(int)(Math.random()*100)+1;//生成三个数
    176         if(mixture[0]==0&&mixture[1]==0)
    177         {
    178         biaodashi[i]=a+"+"+b+"+"+d+" = ";
    179         d1=a+b+d;
    180         }
    181         if(mixture[0]==1&&mixture[1]==1)
    182             {
    183             biaodashi[i]=a+"-"+b+"-"+d+" = ";
    184             d2=a-b;
    185             d1=a-b-d;
    186             }
    187         if(mixture[0]==0&&mixture[1]==1)
    188             {
    189             biaodashi[i]=a+"+"+b+"-"+d+" = ";
    190             d1=a+b-d;
    191             }
    192         if(mixture[0]==1&&mixture[1]==0)
    193             {
    194             biaodashi[i]=a+"-"+b+"+"+d+" = ";
    195             d2=a-b;
    196             d1=a-b+d;
    197             }
    198         }while(d2<0||d1<0); 
    199         answer[i]=d1;
    200         }
    201     if(c==4)
    202         {
    203         d1=a+b;
    204         biaodashi[i]=a+"+"+b+" = ";
    205             while(d1>100)
    206             {
    207             a=(int)(Math.random()*100)+1;
    208             b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99
    209             d1=a+b;
    210             }
    211         biaodashi[i]=a+"+"+b+" = ";
    212         answer[i]=d1;
    213         System.out.print(a+"+"+b+"= ");
    214         }
    215     if(c==1)
    216         {
    217         d1=a-b;
    218             while(d1<0)
    219             {
    220             a=(int)(Math.random()*100)+1;
    221             b=(int)(Math.random()*100)+1;
    222             d1=a-b;
    223             }
    224         biaodashi[i]=a+"-"+b+" = ";
    225         answer[i]=d1;
    226         System.out.print(a+"-"+b+"= ");
    227         }
    228     if(c==2)
    229         {
    230         a=(int)(Math.random()*10);//0-9
    231         b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99
    232         d1=a*b;
    233         while(a<1||b<1||d1>81)
    234             {
    235             a=(int)(Math.random()*10);//0-9
    236             b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99
    237             }
    238         d1=a*b;
    239         biaodashi[i]=a+"*"+b+" = ";
    240         answer[i]=d1;
    241         System.out.print(a+"*"+b+"= ");
    242         }
    243                 if(c==3)
    244             {
    245                 while(a%b!=0)
    246                 {
    247                 a=(int)(Math.random()*100)+1;
    248                 b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99
    249                 }
    250             d1=a/b;    
    251             biaodashi[i]=a+"÷"+b+" = ";
    252             answer[i]=d1;
    253             System.out.print(a+"÷"+b+"= ");
    254             }
    255             }
    256         }
    257     }
    258             
    259         return biaodashi;
    260         }
    261     
    262     
    <%@page import="com.jaovo.msg.model.arithmetic"%>
    <%@ page import="javax.swing.*" %>
    <%@ 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 bgcolor=#FFC4ff onload="load()">
    <%
        //接收客户端传递过来的参数
            request.setCharacterEncoding("UTF-8");
            String time = request.getParameter("usertime");//接收时间
        int time1=0;
        int x=1;
            for(int m=0;m<time.length();m++)
            {
            time1+=(time.charAt(time.length()-m-1)-'0')*x;
            x*=10;
            }//字符串类型的数字转换为整型 成为参数
    %>
    <script>
        var c=1;
        var t;
        var num1=<%=time1%>
        function timeCount()
        {
        document.getElementById("txt").innerHTML=num1-c;
        c=c+1;
        t=setTimeout("timeCount()",1000);
            if(num1==c-1)
            {
            clearTimeout(t);
            alert("同学抱歉时间到了!,点击确定答案将会自动提交");
            load();
            }
        }
        function load()
        { 
        document.getElementById("anniu").click(); 
        }
    window.onload =function(){
    timeCount();//onload 事件会在页面或图像加载完成后立即发生。
    }
    
    </script>
    <h1 style="font-family:华文新魏;font-size:4em" >开始答题</h1>
    <td style="font-family:华文新魏;font-size:1em;500px" align="right">倒计时:</td>
    <p id = "txt"></p>
    <form action="Result.jsp" onsubmit="return validate()==1" method="post">
    <%
        //接收客户端传递过来的参数
        request.setCharacterEncoding("UTF-8");
        String num = request.getParameter("username");//接收出题的数目
        int num1=0;
        x=1;
            for(int m=0;m<num.length();m++)
            {
            num1+=(num.charAt(num.length()-m-1)-'0')*x;
            x*=10;
            }//字符串类型的数字转换为整型 成为参数
            
        arithmetic demo=new arithmetic();//定义对象
        String []biaodashi1=new String[num1];
        biaodashi1=demo.biaodashi(num1);//接收算式
        demo.setsuanshi(biaodashi1);//调用函数 给数据成员算式赋值 以便用于传递
        
            for(int i=0;i<num1;i++)
            {
            out.println(biaodashi1[i]);//输出表达式
    %>
    <input style="80px;height:17px;align="right"" type="text" name="result[<%=i%>]"/> <!-- 答案输入文本框 -->
    <% 
            out.println("<br/>");    
            out.println("<br/>");//换行
            }
            session.setAttribute("jieshou",demo);//用于下一个界面的接收本界面的这个类的全部内容result 所以定义的对象
    %>
        <tr>
    <button id="anniu" onclick="test()" type="submit">提交</button>
    
        </tr>
    </body>
    </html>
     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2 pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5     <head>
     6     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7     <title>出题数目</title>
     8     </head>
     9 <body>
    10     <h1 style="font-family:华文新魏;font-size:5em; 600px"align="right">四则运算挑战</h1>
    11     <body text=red; >
    12     <body  background="‪C:UserszhangDesktop	img.jpg"  >
    13     <!--  img alt="pic" src="C:UserszhangDesktopu=1828030732,3679163737&fm=214&gp=0.jpg"  width = "760px">-->
    14     <form action="chuti.jsp" method="post">
    15     <table align="center" border="0" width="500" style="margin:00px 200px 00px 5px">
    16     <tr>
    17         <td style="font-family:华文;font-size:2em;500px" align="right">选择做数目 </td>
    18         <td>
    19         <input style="100px;height:30px;border:2px solid blue;" type="text" name="username" />
    20         </td>
    21     </tr>
    22     
    23     <tr>
    24         <td style="font-family:华文;font-size:2em;500px" align="right">我们来设置下时间吧: </td>
    25         <td>
    26         <input style="100px;height:30px;border:2px solid blue;" type="text" name="usertime" />
    27         </td>
    28     </tr>
    29     
    30     <tr><td style="150px;height:20px;"></td></tr> <!-- 加了一个自己设置的高度的空行 -->
    31     <tr align="center">
    32     <td colspan="2">
    33     <input style="100px;height:30px;border:3px solid red; margin:00px 20px 00px 350px" type="submit" value="开始答题" />
    34 </body>
    35 </html>
     1 <%@page import="com.jaovo.msg.model.arithmetic"%>
     2 <%@ page import="javax.swing.*" %>
     3 <%@ page language="java" contentType="text/html; charset=UTF-8"
     4 pageEncoding="UTF-8"%>
     5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     6 <html>
     7 <head><title>出题</title></head>
     8 
     9 <body bgcolor=#fFC3D4;>
    10 <!--<  body  background="C:UserszhangDesktop	img.jpg"  >-->
    11 
    12 <h1 style="font-family:华文新魏;font-size:4em">现在来看一下正确答案</h1>
    13 <%
    14 //接收客户端传递过来的参数
    15 arithmetic newdemo=new arithmetic();
    16 newdemo=(arithmetic)session.getAttribute("jieshou");//用于接收CHUti界面传过来的数 (对象)
    17 String []yoursolution=new String[newdemo.shumu];//接收传过来的文本框的答案
    18 int sumright=0,sumerror=0,empty=0;
    19 for(int i=0;i<newdemo.shumu;i++)
    20 {
    21 request.setCharacterEncoding("UTF-8");
    22 out.print(newdemo.suanshi[i]);//正确的算式
    23 yoursolution[i] = request.getParameter("result["+i+"]");//你的答案
    24 out.println(yoursolution[i]);
    25 %>
    26 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
    27 <% 
    28 out.println("正确答案是: ");
    29 out.newLine();
    30 out.println(newdemo.answer[i]);//正确的答案
    31 %>
    32 &nbsp;&nbsp;&nbsp;&nbsp;
    33 <% 
    34 int num1=0;
    35 int x=1;
    36 for(int m=0;m<yoursolution[i].length();m++)
    37 {
    38 num1+=(yoursolution[i].charAt(yoursolution[i].length()-m-1)-'0')*x;
    39 x*=10;
    40 }//字符串类型的数字转换为整型 用于和正确答案比较 因为从出题界面接受的答案是字符串类型
    41 if(yoursolution[i].equals(""))
    42 {
    43     out.newLine();
    44 out.println(" 你没有回答哦!");
    45 out.newLine();
    46 empty++;
    47 }
    48 else if(num1==newdemo.answer[i])
    49 {
    50 sumright++;
    51 out.newLine();
    52 out.println("恭喜你!回答正确!");
    53 out.newLine();
    54 }
    55 else
    56 {
    57 sumerror++;
    58 out.newLine();
    59 out.println("回答错误,再接再厉!");
    60 out.newLine();
    61 }
    62 out.println("<br/>");//换行
    63 }
    64 out.println("回答正确了"+sumright+"道题!");
    65 out.println("<br/>");//换行
    66 out.println("回答错误了"+sumerror+"道题!");
    67 out.println("<br/>");//换行
    68 out.println("没有回答"+empty+"道题!");
    69 out.println("<br/>");//换行
    70 %>
    71 </tr>
    72 <a href="chutijiemian.jsp">退出</a>
    73 </body>
    74 </html>

    运行截图:

    PSP 时间(分钟)
    估计需要多长时间 240
    开发 0
    需求分析(包括学习新知识) 40
    生成设计文档 20
    设计复审 0
    代码规范 10
    具体设计 30
    具体编码 120
    代码复审 0
    测试(自我修复) 30
    测试报告 0
    计算工作量 0
    事后总结并提出改进计划 5
    计划用时 240
    实际用时 255
  • 相关阅读:
    noexcept(c++11)
    右值引用和std::move函数(c++11)
    mint-ui 取值
    apicloud 注意事项
    倒计时
    获取第n天日期
    防止split没有切割的变量报错
    return
    时间戳转为日期
    echarts 中 请求后台改变数据
  • 原文地址:https://www.cnblogs.com/zyt-bg/p/8006512.html
Copyright © 2020-2023  润新知