⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 whatday3.cs

📁 csharp-solution,C#高效编程源码
💻 CS
字号:

using System;

enum MonthName 
{
    January,
    February,
    March,
    April,
    May,
    June,
    July,
    August,
    September,
    October,
    November,
    December
}

class WhatDay 
{
	static void Main() 
	{
	    try {
	        Console.Write("Please input the year: ");
	        string line = Console.ReadLine();
	        int yearNum = int.Parse(line);
	        
	        bool isLeapYear = yearNum % 4 == 0
	                    && yearNum % 100 != 0 || yearNum % 400 == 0;

            int maxDayNum = isLeapYear ? 366 : 365;
	        
    		Console.Write("Please input a day number between 1 and {0}: ", maxDayNum);
    		line = Console.ReadLine();
    		int dayNum = int.Parse(line);

            if (dayNum < 1 || dayNum > maxDayNum) {
                throw new ArgumentOutOfRangeException("Day out of Range");
            }
                
            int monthNum = 0;       
         
            if (isLeapYear) {
                foreach (int daysInMonth in DaysInLeapMonths) {
                    if (dayNum <= daysInMonth) {
                        break;
                    } else {
                        dayNum -= daysInMonth;
                        monthNum++;
                    } 
                }
            } else {   
                foreach (int daysInMonth in DaysInMonths) {
                    if (dayNum <= daysInMonth) {
                        break;
                    } else {
                        dayNum -= daysInMonth;
                        monthNum++;
                    } 
                }
            }
                        
            MonthName temp = (MonthName)monthNum;
            string monthName = temp.Format();
            
            Console.WriteLine("{0} {1}", dayNum, monthName);
        }
        catch (Exception caught) {
            Console.WriteLine(caught);    
        }
    }
    
    // Don't modify anything below here
    static System.Collections.ICollection DaysInMonths 
        = new int[12]{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    static System.Collections.ICollection DaysInLeapMonths 
        = new int[12]{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -