四则运算2.0版程序

一、程序的设计思想:

将之前的1.0版程序改写为网页版。

二、源程序代码:

 welcom.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 xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>在线答题系统</title>
<style type="text/css">
    body {background-image:url(3.jpg);}
</style>
</head>
<body >
    <form id="form2" name="form2" method="post" action="ZhengShu.jsp">
    <div style="text-align:center;color:#F09">
    <br>
    <br>
    <br>
    <br>
    <br>
    <br>
    请输入出题的个数:<input name="amount" type="text" value=""/>    </br></br>
    <input type="submit" value="提交" />
    <input type="reset" value="重置" /> 
    </div>
    </form>
</body>
</html>
View Code

welcomInput.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>Insert title here</title>
</head>
<body>

<%
try
{
    int amount=Integer.parseInt(request.getParameter("amount"));
    session.setAttribute("amount",amount);
    response.setHeader("refresh","0;url = ZhengShu.jsp");
}
catch(Exception e){
    %>
    <script type="text/javascript">
    alert("输入错误,点击确定返回重新输入!!!")
    </script>
    <%
    response.setHeader("refresh","0;url = WelcomeInput.jsp");
}
%>
</body>
</html>
View Code

zhengshu.jsp(产生算术)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.Random" import="java.sql.Connection" 
    import="java.sql.DriverManager" import="java.util.Scanner"
    %>
<!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>
<style type="text/css">
body 
{
    background-image: url(3.jpg);
}
</style>
</head>
<body>


    <%
        String suanshi[]=new String [10000];
        String result1[]=new String[10000];
        int num=Integer.parseInt(request.getParameter("amount"));
        Scanner  in = new Scanner(System.in);
        Random random = new Random();
        int count = 0;
        int result = 0;
        int a=0,b=0;
        String fuhao="***";
        for(int i=0;i<num;i++) {
            count=random.nextInt(4)+1;//随机产生乘除法
            if(count==1) {
                //乘法
                fuhao="*";
                a = random.nextInt(9)+1;
                b = random.nextInt(9)+1;
                result = a*b;
            }
            else if(count==2) {
                //除法
                fuhao="/";
                a = random.nextInt(9)+1;
                b = random.nextInt(9)+1;
                a = a*b;
                result = a/b;
            }
            else if(count==3) {
                //减法
                fuhao="-";
                a = random.nextInt(99)+1;
                b = random.nextInt(a);
                result = a-b;
            }
            else{
                //加法
                fuhao="+";
                a = random.nextInt(99);
                b = random.nextInt(100-a)+1;
                result = a+b;
            }
            suanshi[i]=String.valueOf(a)+fuhao+String.valueOf(b);
            result1[i]=String.valueOf(result);
            request.setAttribute("suanshi", suanshi[i]);
            request.setAttribute("result1",result1[i]);
            request.setAttribute("i",i);
            if((i+1)==num)
            {    
            
%>
    <form id="form2" name="form2" method="post" action="toDatebase.jsp">
    <div style="text-align:center">
    <br>
    <%
    out.print(suanshi[i]+"=");
    %>
    <br>
    请输入答案:<input name="result" type="text" value=""/><br /><br/>
    <input name="" type="submit" value="提交" />
    </div>
    </form>
<%
        }
        else
        {
%>
    <form id="form2" name="form2" method="post" action="toDatebase.jsp">
    <div style="text-align:center">
    <br>
    <%
    out.print(suanshi[i]+"=");
    %>
    <br>
    请输入答案:<input name="result" type="text" value=""/><br /><br/>
    </div>
    </form>
    <%
    }
        }
    %>
</body>
</html>
View Code

三、运行结果截图:

 (1)输入出题个数

  

(2)展示出题结果

四、编程总结及体会:

原文地址:https://www.cnblogs.com/somedayLi/p/7994442.html