第二周

1.p39 实验2 显示当前时间,并输出上午(0-12)好,下午好(13-17),晚上好(18-23)

<%@page import="java.text.SimpleDateFormat"%>
<%@ 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>
    <p style="font-family:黑体;font-size:24">
    <br>显示当前时间<br>
    </p>
    <p>
     <%
        Date d = new Date();//获取系统时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置时间格式
        out.print(sdf.format(d)+"</br>");//输出系统时间
        int h = d.getHours();//得到系统小时数
        if(h>=0&&h<=12){//判断时间范围并输出相应的问候语
            out.print("上午好");
        }else if(h>=13&&h<=17){
            out.print("下午好");
        }else{
            out.print("晚上好");
        }
    %>
        </p>
  </body>
</html>

 

 

2. 使用声明方法,在页面调用该方法,判断2023是不是闰年

-<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!-- 判断是否是闰年 -->
<%!
    boolean isRun(int year){
        if(year % 4 == 0 && year % 100 != 0 || year % 400 ==0){
            return true;
        }else{
            return false;
        }
    }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
2023
<!-- 调用isRun()方法判断 -->
    <%
    if(isRun(2023)){
        out.print("是闰年");
    }else{
        out.print("不是闰年");
    }
    %>

</body>
</html>

  

 

 

3.表达式+程序段 循环输出5条hr,长度分别为100px-500px

<hr width="<%=i%>"

<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%-- 设置一个全局变量 --%>
<%! int i = 0; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
<%-- 循环五次 --%>
<%
for(i = 100; i <= 500; i += 100){
%>
<%-- 设置每次线宽值为i --%>
    <hr width="<%=i%>px"/>
<%} %>
</body>
</html>

  

 

posted @ 2022-03-18 19:57  空?  阅读(25)  评论(0编辑  收藏  举报