四则运算

<%@ 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>

<!-- 居中 -->
<form action="question.jsp"   method="post">
<h2 >请输入题目数量</h2>  
<div>
<input type="text"  name ="quantity"  style="border:1px solid #00a0e9; width:400px; line-height:20px; padding:9px 5px; display:block; float:left;" />
    <input type="submit" style="width:90px; line-height:40px; background-color:#00a0e9; border:none; display:block; float:left; color:#fff;" value="提交" />
</div>
</form>

</body>
</html>

此页面用于输入题目数量,结果:

 

 

 

<%@ 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>
<form action="judge.jsp"   method="post">
<body>
    <%
        //Integer.parseInt(str);   获取题目数量
        int n = Integer.parseInt(request.getParameter("quantity"));
        //out.print(n);
        int a;
        int b;
        //int n=50;
        String operator = "";
        String[] expression = new String[n];
        int[] result = new int[n];
        int[] user = new int[n];
        //1.随机数  2.符号  3.是否可以生成表达式---满足输出以及储存结果  不满足重新生成随机数和表达式
        int i = 0;
        //    for (i = 0; i < n; )
        while (i < n) {
            //生成一个表达式
            a = (int) (1 + Math.random() * 99);
            b = (int) (1 + Math.random() * 99);
            int k = (int) (1 + Math.random() * 3);//1~4对应不同的运算符
            switch (k) {
            case 1:
                operator = "+";
                break;
            case 2:
                operator = "-";
                break;
            case 3:
                operator = "*";
                break;
            case 4:
                operator = "/";
                break;
            }

            //判断表达式是否满足要求
            if (operator.equals("+") && a + b < 100) {
                result[i] = a + b;
                out.print(i+1+"."+a + operator + b + "=" + "</br>");
        %>
                 <input type="text" name=<%="user"+i%>><br>
                 <% 
                i++;
            }
            //out.print(expression[i]);

            else if (operator.equals("-") && a - b >= 0) {
                result[i] = a - b;
                out.print(i+1+"."+a + operator + b + "=" + "</br>");
                %>
                <input type="text" name=<%="user"+i%>><br>
<% 
                i++;
            }

            else if (operator.equals("*") && a * b <= 100) {
                result[i] = a * b;
                out.print(i+1+"."+a + operator + b + "=" + "</br>");
                %>
                <input type="text" name=<%="user"+i%>><br>
            <%     
                i++;
            }

            else if (operator.equals("/") && (a % b == 0) && b != 0) {
                result[i] = a / b;
                out.print(i+1+"."+a + operator + b + "=" + "</br>");
                %>
                            <input type="text" name=<%="user"+i%>><br>
            <%
                i++;
            }
    %>
    
    <%-- <input type="text" name="<%=user[i]%>"> --%>
    <%
        };
    %>
         <%  //session.setAttribute("useras", user);
            session.setAttribute("crctas", result);
            session.setAttribute("num",n); 
            %>
            <br /> <input type="submit" name="submit" value="提交">
</form>
</body>
</html>

此页面用于出题:

 

 

 

<%@ 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>
    <center>
        <%
            //int[] user = (int[]) session.getAttribute("useras");
            int[] result = (int[]) session.getAttribute("crctas");
            int n = (Integer) session.getAttribute("num");
            int []user=new int[n];
            int count = 0;
            for(int i=0;i<result.length;i++)
            {
              int a=Integer.parseInt(request.getParameter("user"+i));
              user[i]=a;
            }
            for (int i = 0; i < n; i++) {
                if (user[i] == result[i]) {
                    out.print("第" + (i + 1 )+ "题回答正确 ");
                    count++;
                } else
                    out.print("第" + (i + 1) + "题回答错误 ,正确答案是:" + result[i]);
                out.println("<br>");
            }
             out.print("总共做对 " + count + "道题,错了 "+(n-count)+"道题");
        %>
    </center>
</body>
</html>

此页面显示判断结果:

 

 

 

总结:

当时有些功能不知道怎么实现,在某位好心人的帮助下,这些雪中送炭的代码摘录下来在这里了哦

 
   <input type="text" name=<%="user"+i%>><br>


<% //session.setAttribute("useras", user); session.setAttribute("crctas", result); session.setAttribute("num",n); %>
          int[] result = (int[]) session.getAttribute("crctas"); int n = (Integer) session.getAttribute("num");

 



posted @ 2019-11-28 19:14  祈欢  阅读(175)  评论(0编辑  收藏  举报