四则运算2

设计思想:

利用随机数生成题目,输入想要完成的数量,定义数组将题目和答案存到数组里,在jsp界面设置出题数量和时间,输入答案提交表单或时间到了自动提交表单,将输入的答案和存在数组里的答案进行对比。

 

源代码:

package 四则运算;
import java.*;
import java.util.Random;

public class YunSuan {
    
    static private int num1;
    static private int num2;
    static private int num3;
    static private int fh1;
    static private int fh2;
    static private int t;
    static private int f;
    static private int w;
    static private int answer[];
    static private String title[];
    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 getNum3() {
        return num3;
    }
    public void setNum3(int num3) {
        this.num3 = num3;
    }
    public int getFh1() {
        return fh1;
    }
    public void setFh1(int fh1) {
        this.fh1 = fh1;
    }
    public int getFh2() {
        return fh2;
    }
    public void setFh2(int fh2) {
        this.fh2 = fh2;
    }
    public int getT() {
        return t;
    }
    public void setT(){
        t++;
    }
    public int getF() {
        return f;
    }
    public void setF() {
        f++;
    }
    public int getW()
    {
        return w;
    }
    public void setW()
    {
        w++;
    }
    public String[] getTitle() {
        return title;
    }
    public void setTitle(String[] title) {
        this.title = title;
    }
    public int[] getAnswer() {
        return answer;
    }
    public void setAnswer(int answer[]) {
        this.answer = answer;
    }
    


    
    public void fuzhi(int n)
    {
        Random rd=new Random();
        String title[] = new String[n];
        int answer[]=new int[n];
        for(int i=0;i<n;i++)
        {
            setNum1(rd.nextInt(100)+1);
            setNum2(rd.nextInt(100)+1);
            setNum3(rd.nextInt(100)+1);
            setFh1(rd.nextInt(4));
            setFh2(rd.nextInt(4));
            if(getFh1()<2&&getFh2()<2)
            {
               if(getFh1()==0&&getFh2()==0)
               {
                   int daan=getNum1()+getNum2()+getNum3();
                   if(daan<=100)
                   {
                       answer[i]=daan;
                       title[i]=getNum1()+"+"+getNum2()+"+"+getNum3();
                   }
                   else 
                   {
                       i--;
                   }
               }
               else if(getFh1()==0&&getFh2()==1)
               {
                   int daan=getNum1()+getNum2()-getNum3();
                   if(daan>=0&&daan<=100)
                   {
                       answer[i]=daan;
                       title[i]=getNum1()+"+"+getNum2()+"-"+getNum3();
                   }
                   else 
                   {
                       i--;
                   }
               }
               else if(getFh1()==1&&getFh2()==0)
               {
                   int daan=getNum1()-getNum2()+getNum3();
                   if(daan>=0&&daan<=100)
                   {
                       answer[i]=daan;
                       title[i]=getNum1()+"-"+getNum2()+"+"+getNum3();
                   }
                   else 
                   {
                       i--;
                   }
               }
               else if(getFh1()==1&&getFh2()==1)
               {
                   int daan=getNum1()-getNum2()-getNum3();
                   if(daan>=0&&daan<=100)
                   {
                       answer[i]=daan;
                       title[i]=getNum1()+"-"+getNum2()+"-"+getNum3();
                   }
                   else 
                   {
                       i--;
                   }
               }
            }
            else
            {
            
               if(getFh1()==0)
               {
                 int daan=getNum1()+getNum2();
                 if(daan<=100)
                 {
                     answer[i]=daan;
                     title[i]=getNum1()+"+"+getNum2();
                 }
                 else 
                 {
                     i--;
                 }
                   
               }
               else if(getFh1()==1)
               {
                  int daan=getNum1()-getNum2();
                  if(daan>=0)
                  {
                      answer[i]=daan;
                      title[i]=getNum1()+"-"+getNum2();
                  }
                  else
                  {
                      i--;
                  }
                  
               }
               else if(getFh1()==2)
               {
                   setNum1(rd.nextInt(10)+1);
                      setNum2(rd.nextInt(10)+1);
                   answer[i]=getNum1()*getNum2();
                   title[i]=getNum1()+"*"+getNum2();
               }
               else if(getFh1()==3)
               {
                   if(getNum1()%getNum2()==0)
                   {
                       answer[i]=getNum1()/getNum2();
                       title[i]=getNum1()+"/"+getNum2();
                   }
                   else
                   {
                       i--;
                   }
                   
               }

            }
            setAnswer(answer);
            setTitle(title);
        }
        
    }

    public static void main(String[] args) {
        YunSuan ys=new YunSuan();
                ys.fuzhi(1000);
      
    }

}

 

<%@ 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  style="background-image:url('../image/back.jpg');">
<form action="page.jsp" method="get">
<center style="color:red ; font-family:华文行楷;font-size:35px">四则运算</center>
<%
String nn=(String)request.getAttribute("error");
 if(nn==null)
 {
     request.setAttribute("error", "   ");
 }
%>
<br>
<br>
<h1 style="margin-left:360px;font-family:楷体;font-size:15px"> 出题数量 <input  type="text"  name="number" /><%=request.getAttribute("error")%></h1>
<br>
<h1 style="margin-left:360px;font-family:楷体;font-size:15px"> 答题时间(秒) <input  type="text"  name="time" /><%=request.getAttribute("error")%></h1>
<br>
<center><input type="submit" value="确定" /></center>
</form>
</body>          
</html>

 

<%@page import="四则运算.YunSuan"%>
<%@ 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 style="background-image:url('../image/back.jpg');">

<form action="page2.jsp" method="get">
<center style="color:red ; font-family:华文行楷;font-size:35px">四则运算</center>
<br>
<br>
<%
String num=request.getParameter("number");
String time=request.getParameter("time");
if(num == null || "".equals(num.trim())||time == null || "".equals(time.trim())){
    request.setAttribute("error", "这一行不能为空");

%>
<jsp:forward page="choose.jsp"></jsp:forward>
<%
}
int number=Integer.parseInt(num);
int time2=Integer.parseInt(time);
YunSuan ys=new YunSuan();
ys.fuzhi(number);
%>

 <center>时间已经过去:<span id=id2>0</span>秒;<%=time2 %>秒后系统自动提交。</center>>
<script language="javascript">
var i=0;
function showtime(){
  i=i+1;
  id2.innerHTML=i;
  setTimeout("showtime()",1000);
  if(i==<%=time2%>)
      document.forms[0].submit();
}
showtime();
</script>


<br>
<br>
<%
   for(int i=0;i<number;i++)
   {
%>
<h1 style="margin-left:360px;font-family:楷体;font-size:15px"> <%=ys.getTitle()[i]%> <input  type="text"  name=<%=i %>/></h1>
<br>
<%
   }

%>

<center><input type="submit" value="提交" /></center>
<br>



</form>
</body>
</html>

 

<%@page import="java.util.*"%>
<%@page import="四则运算.YunSuan"%>
<%@ 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 style="background-image:url('../image/back.jpg');">
<center style="color:red ; font-family:华文行楷;font-size:35px">四则运算</center>
<br>
<br>
<%
YunSuan ys=new YunSuan();
int i=0;
Enumeration  pNames=request.getParameterNames();
while(pNames.hasMoreElements()){
    String name=(String)pNames.nextElement();
    String value=request.getParameter(name);
    if(value == null || "".equals(value))
    {
        request.setAttribute("error", "未做");
        ys.setW();
        %>
    
    <h1 style="margin-left:360px;font-family:楷体;font-size:15px"> <%=ys.getTitle()[i]%> <input  type="text"  name=<%=i %> value=<%=request.getAttribute("error")%>>×&nbsp;正确答案为:<%=ys.getAnswer()[i] %></h1>
   <br>
    <% 
    }
   else
   {
       int answer=Integer.parseInt(value);
    if(ys.getAnswer()[i]==answer)
    {
        ys.setT();
   %>
   <h1 style="margin-left:360px;font-family:楷体;font-size:15px"> <%=ys.getTitle()[i]%> <input  type="text"  name=<%=i %> value=<%=answer %>>√</h1>
    <br>
   <%

    }
    else 
    {
        ys.setT();
           %>
           <h1  style="margin-left:360px;font-family:楷体;font-size:15px"> <%=ys.getTitle()[i]%> <input  type="text"  name=<%=i %> value=<%=answer %>>×&nbsp;正确答案为:<%=ys.getAnswer()[i] %></h1>
            <br>
           <%
    }
   
}
    i++;
}

%>
<br>
 <h1  style="margin-left:360px;font-family:楷体;font-size:20px;color:red"> 共答对<%=ys.getT() %>题</h1>
 <h1  style="margin-left:360px;font-family:楷体;font-size:20px;color:red"> 共答错<%=ys.getF() %>题</h1>
 <h1  style="margin-left:360px;font-family:楷体;font-size:20px;color:red"> 未答<%=ys.getW() %>题</h1>
</body>
</html>

 

截图:

 

 

 

psp

PSP2.1 Personal Software Process Stages Time(h)
Planning 计划  
 Estimate 估计这个任务需要多少时间 5
Development 开发  
 · Analysis · 需求分析 (包括学习新技术) 0.5
  · Design Spec   · 生成设计文档 0.5
  · Design Review  · 设计复审 (和同事审核设计文档) 0.5
 ·Coding Standard   · 代码规范 (为目前的开发制定合适的规范) 0.5
  · Design  · 具体设计 1
 · Coding · 具体编码 2
  · Code Review · 代码复审 0.5
  · Test  测试(自我测试,修改代码,提交修改) 1
Reporting 报告  
  · Test Report   · 测试报告 1
  · Size Measurement   · 计算工作量 0.5
  · Postmortem & Process Improvement Plan   · 事后总结, 并提出过程改进计划 0.5
 

合计

8.5

 

posted @ 2017-12-06 15:51  但为君故。  阅读(167)  评论(0编辑  收藏  举报