vs2015 dynamicweb3-17 闰年

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="dynamicweb3_17.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dynamicweb3_17
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //是否闰年
        public static bool IsLeap(int year)
        {
            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //哪个季节
        public static string Season(int month)
        {
            switch (month)
            { 
                case 12:
                case 1:
                case 2:
                    return "冬季"; //函数返回,不再使用break
                case 3:
                case 4:
                case 5:
                    return "春季";
                case 6:
                case 7:
                case 8:
                    return "夏季";
                case 9:
                case 10:
                case 11:
                    return "秋季";
                default:
                    return "错误";
            }
        }

        //月份有几天
        public static int DaysOfMonth(int year, int month)
        {
            switch (month)
            { 
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                case 2:
                    if (IsLeap(year))
                    {
                        return 29;  // 闰年 2月 有29 天
                    }
                    else
                    {
                        return 28;
                    }
                default:
                    return -1;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int year = DateTime.Now.Year;
            int month = DateTime.Now.Month;

            if (WebForm1.IsLeap(year))
            {
                Label1.Text = year + "年是闰年。<br>";
            }
            else
            {
                Label1.Text = year + "年不是闰年。<br>";
            }

            Label1.Text += month + "月份属于" + WebForm1.Season(month) + "<br>";

            Label1.Text += year + "年" + month + "月有" + DaysOfMonth(year, month) + "天。";
        }
    }
}

猜你喜欢

转载自blog.csdn.net/modern358/article/details/115391435