软件工程结对作业01

  1. 设计思想:设置出题数目,出题。JSP 设置出题界面,做题界面,结果界面,判断答案是否正确时,用数组接收对比。
  2. 源程序代码:

Arithmetic.java

package com.jaovo.msg.model;

 

public class arithmetic

{

       public int []answer;//答案

       public int shumu;//出题数目

       public String []suanshi;//算式

       public void setsuanshi(String []suanshi)

       {

              this.suanshi=suanshi;

       }

      

       public String [] biaodashi(int n)

       {

              shumu=n;

              answer=new int[n];

              int a,b,c,d1 = 0,d,d2=0;

              int []mixture=new int[2];

              String []biaodashi=new String[n];

      

              for(int i=0;i<n;i++)

              {

                     a=(int)(Math.random()*100)+1;//1-100

                     b=(int)(Math.random()*100)+1;

                     c=(int)(Math.random()*5)+1;//随机生成一个1-5的整数,4表示加法,1表示减法,2表示乘法,3表示除法,5表示混合

                     if(c==5)//混合运算

                     {

                            do

                            {    

                                   for(int m=0;m<2;m++)

                                   {

                                          mixture[m]=(int)(Math.random()*2);//0-1

                                   }//控制运算符

      

                                   a=(int)(Math.random()*100)+1;

                                   b=(int)(Math.random()*100)+1;

                                   d=(int)(Math.random()*100)+1;//生成三个数

                                   if(mixture[0]==0&&mixture[1]==0)

                                   {

                                          biaodashi[i]=a+"+"+b+"+"+d+" = ";

                                          d1=a+b+d;

                                   }

                                   if(mixture[0]==1&&mixture[1]==1)

                                   {

                                          biaodashi[i]=a+"-"+b+"-"+d+" = ";

                                          //d2=a-b;

                                          d1=a-b-d;

                                   }

                                   if(mixture[0]==0&&mixture[1]==1)

                                   {

                                          biaodashi[i]=a+"+"+b+"-"+d+" = ";

                                          d1=a+b-d;

                                   }

                                   if(mixture[0]==1&&mixture[1]==0)

                                   {

                                          biaodashi[i]=a+"-"+b+"+"+d+" = ";

                                          //d2=a-b;

                                          d1=a-b+d;

                                   }

                            }while(d1<0);

                            answer[i]=d1;

                     }

                     if(c==4)//单加法

                     {

                            d1=a+b;

                            biaodashi[i]=a+"+"+b+" = ";

                            while(d1>100)

                            {

                                   a=(int)(Math.random()*100)+1;

                                   b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99

                                   d1=a+b;

                            }

                            biaodashi[i]=a+"+"+b+" = ";

                            answer[i]=d1;

                            //System.out.print(a+"+"+b+"= ");

                     }

                     if(c==1)//单减法

                     {

                            while(a-b<0)

                            {

                                   a=(int)(Math.random()*100)+1;

                                   b=(int)(Math.random()*100)+1;

                            }

                            d1=a-b;

                            biaodashi[i]=a+"-"+b+" = ";

                            answer[i]=d1;

                            //System.out.print(a+"-"+b+"= ");

                     }

                     if(c==2)//乘法

                     {

                            a=(int)(Math.random()*10);//0-9

                            b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99

                            //d1=a*b;

                            while(a<1||b<1||d1>81)

                            {

                                   a=(int)(Math.random()*10);//0-9

                                   b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99

                            }

                            d1=a*b;

                            biaodashi[i]=a+"*"+b+" = ";

                            answer[i]=d1;

                            //System.out.print(a+"*"+b+"= ");

                     }

                     if(c==3)//除法

                     {

                            //d1=a/b;

                            while(a%b!=0||a/b>9||(a<=81&&b>=10)||(a>9&&a==b)||(a>81))

                            {

                                   a=(int)(Math.random()*100)+1;

                                   b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99

                            }

                            d1=a/b;

                            biaodashi[i]=a+"÷"+b+" = ";

                            answer[i]=d1;

                            //System.out.print(a+"÷"+b+"= ");

                     }

             

                     //查重

                     for(int k=i-1;k>=0;k--)

                     {

                            while(biaodashi[i].equals(biaodashi[k]))

                            {

                                   a=(int)(Math.random()*100)+1;//1-100

                                   b=(int)(Math.random()*100)+1;

                                   c=(int)(Math.random()*5)+1;//随机生成一个1-5的整数,4表示加法,1表示减法,2表示乘法,3表示除法,5表示混合

                                   if(c==5)

                                   {

                                          do//混合运算

                                          {    

                                                 for(int m=0;m<2;m++)

                                                 {

                                                        mixture[m]=(int)(Math.random()*2);//0-1

                                                 }//控制运算符

             

                                                 a=(int)(Math.random()*100)+1;

                                                 b=(int)(Math.random()*100)+1;

                                                 d=(int)(Math.random()*100)+1;//生成三个数

                                                 if(mixture[0]==0&&mixture[1]==0)

                                                 {

                                                        biaodashi[i]=a+"+"+b+"+"+d+" = ";

                                                        d1=a+b+d;

                                                 }

                                                 if(mixture[0]==1&&mixture[1]==1)

                                                 {

                                                        biaodashi[i]=a+"-"+b+"-"+d+" = ";

                                                        d2=a-b;

                                                        d1=a-b-d;

                                                 }

                                                 if(mixture[0]==0&&mixture[1]==1)

                                                 {

                                                        biaodashi[i]=a+"+"+b+"-"+d+" = ";

                                                        d1=a+b-d;

                                                 }

                     if(mixture[0]==1&&mixture[1]==0)

                            {

                            biaodashi[i]=a+"-"+b+"+"+d+" = ";

                            d2=a-b;

                            d1=a-b+d;

                            }

                                          }while(d2<0||d1<0);

                                          answer[i]=d1;

                                   }

                                   if(c==4)

                                   {

                                          d1=a+b;

                                          biaodashi[i]=a+"+"+b+" = ";

                                          while(d1>100)

                                          {

                                                 a=(int)(Math.random()*100)+1;

                                                 b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99

                                                 d1=a+b;

                                          }

                                          biaodashi[i]=a+"+"+b+" = ";

                                          answer[i]=d1;

                                          //System.out.print(a+"+"+b+"= ");

                                   }

                                   if(c==1)

                                   {

                                          d1=a-b;

                                          while(d1<0)

                                          {

                                                 a=(int)(Math.random()*100)+1;

                                                 b=(int)(Math.random()*100)+1;

                                                 d1=a-b;

                                          }

                                          biaodashi[i]=a+"-"+b+" = ";

                                          answer[i]=d1;

                                          //System.out.print(a+"-"+b+"= ");

                                   }

                                   if(c==2)

                                   {

                                          a=(int)(Math.random()*10);//0-9

                                          b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99

                                          d1=a*b;

                                          while(a<1||b<1||d1>81)

                                          {

                                                 a=(int)(Math.random()*10);//0-9

                                                 b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99

                                          }

                                          d1=a*b;

                                          biaodashi[i]=a+"*"+b+" = ";

                                          answer[i]=d1;

                                          //System.out.print(a+"*"+b+"= ");

                                   }

                                   if(c==3)

                                   {

                                          while(a%b!=0)

                                          {

                                                 a=(int)(Math.random()*100)+1;

                                                 b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99

                                          }

                                          d1=a/b;

                                          biaodashi[i]=a+"÷"+b+" = ";

                                          answer[i]=d1;

                                          //System.out.print(a+"÷"+b+"= ");

                                   }

                            }

                     }

              }

              return biaodashi;

       }

 

}

chuti.jsp

<%@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  background="C:UsersLENOVODesktop6a7501ab858e215d.jpg">

<%

       //接收客户端传递过来的参数

       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>

<tdstyle="font-family:华文新魏;font-size:1em;width:500px" align="right">倒计时:</td>

<p id = "txt"></p>

<formaction="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;border-color:red;align="right"" type="text" name="result[<%=i%>]"/> <!-- 答案输入文本框 -->

              <%

                     out.println("<br/>");  

                     out.println("<br/>");//换行

       }

       session.setAttribute("jieshou",demo);//用于下一个界面的接收本界面的这个类的全部内容result 所以定义的对象

%>

<tr>

<buttonid="anniu"onclick="test()"type="submit">提交</button>

 

</tr>

</body>

</html>

chutijiemian.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>出题数目</title>

</head>

<body>

<h1 style="font-family:华文新魏;font-size:5em; 800px" align="right"></h1>

<tr><td style="150px;height:40px;"></td></tr> <!-- 加了一个自己设置的高度的空行 -->

<body text=blue; >

<body  background="C:UsersLENOVODesktop21457964_143221115623_2.jpg">

 

<formaction="chuti.jsp"method="post">

<tablealign="center"border="0"width="500"style="margin:00px 200px 00px 5px">

<tr>

<td style="font-family:华文;font-size:2em;500px" align="right">你想做几道题: </td>

<td>

<input style="100px;height:30px;" type="text" name="username" />

</td>

</tr>

 

<tr>

<td style="font-family:华文;font-size:2em;500px" align="right">规定下做题时间吧: </td>

<td>

<input style="100px;height:30px;" type="text" name="usertime" />

</td>

</tr>

 

<tr><td style="150px;height:40px;"></td></tr> <!-- 加了一个自己设置的高度的空行 -->

<tr align="center">

<td colspan="2">

<input style="100px;height:30px; margin:00px 20px 00px 150px" type="submit" value="开始答题" />

</body>

</html>

Result.jsp

<%@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  background="C:UsersLENOVODesktop005018466416236_b.jpg">

<img alt="pic" src="C:UsersLENOVODesktop611aaec384455c55.gif"  width = "200px" align="center">

<body bgcolor=#7FFFC4>

<h1 style="font-family:华文新魏;font-size:5em">做题情况如下:</h1>

<%

//接收客户端传递过来的参数

       arithmetic newdemo=new arithmetic();

       newdemo=(arithmetic)session.getAttribute("jieshou");//用于接收CHUti界面传过来的数 (对象)

       String []yoursolution=new String[newdemo.shumu];//接收传过来的文本框的答案

       int sumright=0,sumerror=0,empty=0;

       for(int i=0;i<newdemo.shumu;i++)

       {

              request.setCharacterEncoding("UTF-8");

              out.print(newdemo.suanshi[i]);//正确的算式

              yoursolution[i] = request.getParameter("result["+i+"]");//你的答案

              out.println(yoursolution[i]);

%>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>

<%

              out.println("正确答案是: ");

              out.println(newdemo.answer[i]);//正确的答案

%>

&nbsp;&nbsp;&nbsp;&nbsp;

<%

              int num1=0;

              int x=1;

              for(int m=0;m<yoursolution[i].length();m++)

              {

                     num1+=(yoursolution[i].charAt(yoursolution[i].length()-m-1)-'0')*x;

                     x*=10;

              }//字符串类型的数字转换为整型 用于和正确答案比较 因为从出题界面接受的答案是字符串类型

              if(yoursolution[i].equals(""))

              {

                     out.println("时间太快?你没有回答哦!");

                     out.newLine();

                     empty++;

              }

              else if(num1==newdemo.answer[i])

              {

                     sumright++;

                     out.println("恭喜你!回答正确!");

                     out.newLine();

              }

              else

              {

                     sumerror++;

                     out.println("回答错误,再接再厉!");

                     out.newLine();

              }

              out.println("<br/>");//换行

              }

              out.println("回答正确了"+sumright+"道题!");

              out.println("<br/>");//换行

              out.println("回答错误了"+sumerror+"道题!");

              out.println("<br/>");//换行

              out.println("没有回答"+empty+"道题!");

              out.println("<br/>");//换行

%>

<script>

       function tongji()

       {

              var se=confirm("确定要查询做题情况吗?");

              if (se==true)

                     {

                            alert("你好棒啊!!恭喜你回答正确了 道题!回答错误了 道题!没有回答 道题!你真的好棒啊!!!" );

                     }

              else

                     {

                            alert("这么有信心?你好聪明哦!");

                     }

       }

</script>

<input type="button" onclick="tongji()" value="是否查看做题情况" />

</tr>

<a href="chutijiemian.jsp">退出</a>

</body>

</html>

运行结果截图

 

 
原文地址:https://www.cnblogs.com/liurx/p/7994549.html