日期类Date控制台应用程序设计C#

日期类Date控制台应用程序设计

小咸鱼终于又准备敲代码辽呜呜,什么王者荣耀,耽误我学习hhh

实验要求

实验要求:写出类 Date 的定义代码,含有如图所示的 3 个字段及 4 个方法。

实验目的

实验目的: 学会自定义类及类成员并实例化对象解决实际问题。

实验内容

(1) 在VS开发环境的控制台应用中,从菜单“项目”→“添加类”,添加自定义的类Date。

定义类的字段year,month,day用于记录年月日信息。

(2)定义类的构造方法Date,用于初始化日期对象为公元元年元旦。

internal Date()

{        year=1; month=1; day=1;         }

(3)定义五个方法分别完成显示日期、判断闰年、

获取年中天数、获取是星期几。

internal string DisplayDate()

bool IsLeapYear(int year) //判断该年是否为闰年

internal bool IsValidDateValue()

int GetDayOfYear()

public string GetDayOfWeek()

(4)对类Date的构造方法及DisplayDate方法进行重载,

接受与显示用户输入的日期。

internal Date(int year,int month,int day)

internal string DisplayDate(int year, int month, int day)

(5)实例化Date类,访问类的方法显示日期及日期是星期几。
在这里插入图片描述

代码实现

Date.cs代码

class Date
{
    int year, month, day;
    public Date()
    {
        year = 1; month = 1; day = 1;
    }
    public Date(int year, int month, int day)
    {
        this.year = year; this.month = month; this.day = day;
    }
    internal string DisplayDate()
    {
        return year + "-" + month + "-" + day;
    }
    internal string DisplayDate(int year, int month, int day)
    {
        return "公元" + year + "年" + month + "月" + day + "日";
    }
    //static
    internal bool IsLeapYear(int year)//判断该年是否为闰年
    {
        bool isLeap = false;
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
            isLeap = true;
        return isLeap;
    }
    //static
    internal int GetDayOfYear()
    {
        int dayOfYear = 0;
        int[] monthDaysEachYear = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int[] monthDaysLeapYear = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (IsLeapYear(year))
        {
            for (int i = 0; i < month; i++)
                dayOfYear += monthDaysLeapYear[i];
        }
        else
        {
            for (int i = 0; i < month; i++)
                dayOfYear += monthDaysEachYear[i];
        }
        dayOfYear += day;
        return dayOfYear;
    }

    internal bool IsValidDateValue()
    {
        if (month < 1 || month > 12)
        {
            return false;
        }
        if (day < 1 || day > 31)
        {
            return false;
        }
        if ((month == 4 || month == 6 || month == 9 || month == 11) && (day == 31))
        {
            return false;
        }
        if (month == 2)
        {
            bool leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
            if (day > 29 || (day == 29 && !leap))
            {
                return false;
            }
        }
        return true;
    }
    internal string GetDayOfWeek()
    {
        int daysCount = 0;
        for (int i = 1; i < year; i++)
        {
            if (IsLeapYear(i))
                daysCount += 366;
            else
                daysCount += 365;
        }
        daysCount += GetDayOfYear();
        string weekDay = "星期" + "日一二三四五六"[(daysCount % 7)];
        return weekDay;
    }
}

Program.cs代码

using System;
class TestDate
{
    static void Main(string[] args)
    {
        Console.WriteLine("欢迎使用Data类,C#程序设计by SDNU咸鱼小十七酱ovo");
        Date dateNoArgs = new Date();//声明及实例化对象
        Console.WriteLine("设定的初始日期是:" + dateNoArgs.DisplayDate());
        Console.WriteLine("公元元年元旦是:" + dateNoArgs.GetDayOfWeek());
        int yearValue, monthValue, dayValue;
        Console.WriteLine("请依次输入年、月、日:");
        Console.WriteLine("请输入年:");
        yearValue = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("请输入月:");
        monthValue = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("请输入日:");
        dayValue = Convert.ToInt32(Console.ReadLine());
        Date userInputDate = new Date(yearValue, monthValue, dayValue);//声明及实例化对象
        if (userInputDate.IsValidDateValue())
        {
            Console.WriteLine("输入的日期是:" + userInputDate.DisplayDate());
            Console.WriteLine("输入的日期是:" + userInputDate.DisplayDate(yearValue, monthValue, dayValue));
            Console.WriteLine(userInputDate.DisplayDate() + "是" + userInputDate.GetDayOfWeek());
        }
        else
            Console.WriteLine("您输入的日期不正确,无法完成计算!");
        Console.ReadLine();

    }
}

运行结果

CSharp程序调试运行界面截屏//调试平台:Visual Studio 2019

收获与体会

学会了自定义类。Date.cs用了86行,Program.cs用了30行。借鉴了老师对于实验日期类Date控制台应用程序设计的视频讲解中的代码。对于C#的基本知识了解的还是太少,还需要多看课本,熟悉课本,才能更好的写代码,要继续加油喔!

有问题欢迎指正说明,并联系QQ2062642718

发布了6 篇原创文章 · 获赞 12 · 访问量 289

猜你喜欢

转载自blog.csdn.net/zhaoyananzyn/article/details/104937060