1. 程式人生 > >軟工概論第二周個人項目四則運算二(改進)

軟工概論第二周個人項目四則運算二(改進)

添加 setattr login session i++ idt new 數組 w3c

設計思想:

設計思想:輸出表達式的個數n,利用隨機數方法產生小於100的隨機數,用for循環產生多個隨機數,然後在進行判斷,判斷兩個證書的和小於100,差大於0,乘積小於100,能夠整除如果不符合要求則跳過本次循環,如果符合則賦值當總數到n時跳出循環,用字符串保存表達式,用double類型的數組保存正確結果,然後循環輸出表達式,輸入結果後跳轉到判斷結果界面,循環判斷結果,如果相等flag[]為1,不等為0,最後給出正確的題目數量和錯誤的數量和未作的數量,在循環輸出不正確的那些題目

改進的地方:表達式數量輸入為空或者為0的優化,結果輸入時意外添加空格的優化,最後輸出不正確的題目,在線答題時時間限制,更加人性化

//
Suanshijava package p1; public class Suanshi { final int MAX=1000; double[] e= new double[MAX]; String[] f=new String[MAX]; private int num; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public Suanshi(){}
public Suanshi(int n) { creatSuanshi(n); this.num=n; } public void creatSuanshi(int n) { int a; int c; int d; for(int i=0;i<n;i++) { a=(int)(Math.random()*100+1); d=(int)(Math.random()*4+1); c
=(int)(Math.random()*100+1); switch(d){ case 1: if(a+c>100) { i--; break; } e[i]=a+c; f[i]=""+a+"+"+c; break; case 2: if(a-c<0) { i--; break; } e[i]=a-c; f[i]=""+a+"-"+c; break; case 3: while(a*c>100) { a=(int)(Math.random()*100+1); c=(int)(Math.random()*100+1); } e[i]=a*c; f[i]=""+a+"*"+c; break; case 4: e[i]= (int)(Math.random()*10+1); c= (int)(Math.random()*10+1); a=(int)(c*e[i]); f[i]=""+a+"/"+c; if((c==0)&&(a>100)) { i--; break; } break; } } } public String getSuanshi(int i) { return f[i]; } public double getDaan(int i) { return e[i]; } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
      <title>輸入數量頁面</title>    
  </head>
    <body background="C:\Users\DELL\Desktop\photo1.jpg">
    <br><br><br><br><br><br><br>
<%
    String loginError = (String)request.getAttribute("loginError");
    if(loginError == null)
    {
        loginError = "";
    }
%>
        <form action="add.jsp" method="get">
            <center><h1>輸入頁面</h1>
            數量:<input type="text" name="number"/><%=loginError %><br><br>
            時間一共為<font color="red">100</font>秒<br><br>
            <input type="submit" value="開始答題" /></center>
        </form>
    </body>
</html>
<%@page import="p1.Suanshi"%>
<%@ 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 onLoad="TimeClose();">
<script language="javascript">
    var cTime=100;
    function TimeClose()
    {
        window.setTimeout(TimeClose(),1000);
        if(cTime<=0)
            CloseWindow_Click();
        this.ShowTime.innerHTML="倒計時"+cTime+"秒後結束答題";
        cTime--;
    }
    function CloseWindow_Click()
    {
        window.close();
    }
</script>
<%
    //獲取客戶端傳遞過來的參數
    
    if("".equals(request.getParameter("number").trim()))
    {
    request.setAttribute("loginError","不能為空");
%>
    <jsp:forward page="index.jsp"></jsp:forward>
<%
    }
    else 
    {
        if(Integer.parseInt(request.getParameter("number").trim())==0)
        {
            request.setAttribute("loginError","不能為0");
%>
            <jsp:forward page="index.jsp"></jsp:forward>
<%
        }        
    }
    int number=Integer.parseInt(request.getParameter("number").trim());
       Suanshi suanshi=new Suanshi(number);
       session.setAttribute("suanshi", suanshi);
%>    
    <center><h1>答題</h1></center>
       <div align="right" id="ShowTime">倒計時100秒後結束答題</div>
       <hr>
       <form action="judge.jsp">

       <table align="center" border="1" width="450">
       <tr>
    <td align="center">題號</td>
    <td align="center">表達式</td>
    <td align="center">你的答案</td>
    </tr>
<% 
    for(int i=0;i<number;i++)
     {
%>    
    <tr>
    <td align="center">第<%=i+1%>題</td>
    <td align="center"><%=suanshi.getSuanshi(i)%>=</td>
    <td><input type="text" name="daan<%=i%>"></td>
    </tr>
<% 
     }
%>
    </table><br>
    <center><input type="submit" value="提交"></center>
    </form>
</body>
</html>
<%@page import="p1.Suanshi"%>
<%@ 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>
<%
    Suanshi suanshi = (Suanshi)session.getAttribute("suanshi") ;
    int dui=0;
    int cuo=0;
    int meizuo=0; 
    int[] flag=new int[suanshi.getNum()];
    String daan[]=new String[suanshi.getNum()];
    double daan1[]=new double[suanshi.getNum()];
    
    for(int i=0;i<suanshi.getNum();i++)
    { 
        daan[i]=request.getParameter("daan"+i).trim();
        if((daan[i]==null)||("".equals(daan[i].trim())))
        {    
            flag[i]=0;
            meizuo++;
        }
        else
        {
    
              daan1[i] =Double.parseDouble(daan[i]);
            if(Math.abs(daan1[i]-suanshi.getDaan(i))<0.01)
            {
                flag[i]=1;
                dui++;
            }
            else
            {
                flag[i]=0;
                cuo++;
            }
        }
    }
%>
<body>
    <hr size="1" color="black">
    <center>
        <h1>測試成績</h1>
    <hr size="1" color="black">
        一共:<%=suanshi.getNum()%>道<br>
        沒做的:<%=meizuo %>道<br>
        做錯的:<%=cuo %>道<br>
        作對的:<%=dui %>道<br></center>
    <hr width="800" color="red">
        <center><h3>錯誤題目</h3></center>
    <hr width="800" color="red">
<table align="center" border="1" width="450">
<tr>
    <td align="center">題號</td>
    <td align="center">表達式</td>
    <td align="center">正確答案</td>
    <td align="center">你的結果</td>
    </tr>
<% 
    for(int i=0;i<suanshi.getNum();i++)
    if(flag[i]==0)
     {
%>
        <tr>
        <td align="center">第<%=i+1%>題</td>
        <td align="center"><%=suanshi.getSuanshi(i)%>=</td>
        <td align="center"><%=suanshi.getDaan(i)%></td>
        <td align="center"><%=daan[i]%></td>
        </tr>
<% 
     }
%>
</table>
</body>
</html>

軟工概論第二周個人項目四則運算二(改進)