日期類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();
}
}
運行結果
收穫與體會
學會了自定義類。Date.cs用了86行,Program.cs用了30行。借鑑了老師對於實驗日期類Date控制檯應用程序設計的視頻講解中的代碼。對於C#的基本知識瞭解的還是太少,還需要多看課本,熟悉課本,才能更好的寫代碼,要繼續加油喔!
有問題歡迎指正說明,並聯系QQ2062642718