jsp第一次作业

1.编写jsp页面,计算1-100之间的所有素数之和

提示:素数:在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除的数

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>

<body>
    求1-100的素数和
    <br>
    <%
        int sum = 0;
        int[] a = new int[100];
        for (int i = 0; i < a.length; i++) {
            a[i] = i;
        }
        for (int i = 2; i < a.length; i++) {
            boolean flag = true;
            for (int j = 2; j < a[i]; j++) {
                if (a[i] % j == 0)
                    flag = false;
            }
            if (flag) {
                sum += i;
            }

        }
    %>
    1-100之间的素数和是:
    <%=sum%>
</body>
</html>

 2.   编写jsp页面,计算1-100之间的所有偶数之和

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>

<body>
    求1-100的偶数和
    <br>
    <%
        int sum = 0;
        int[] a = new int[101];
        for (int i = 0; i < a.length; i++) {
            a[i] = i;
        }
        for (int i = 0; i < a.length; i++) {
            if (a[i] % 2 == 0) {
                sum += i;
            }
        }
    %>
    1-100之间的偶数和是:
    <%=sum%>
</body>
</html>

原文地址:https://www.cnblogs.com/menfanbo/p/14475790.html