首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#第2周实验-任务9-编撰一个控制台应用-输入一个日期,求该日期是这一年中的第几天

2012-09-23 
C#--第2周实验--任务9--编写一个控制台应用--输入一个日期,求该日期是这一年中的第几天/* (程序头部注释开

C#--第2周实验--任务9--编写一个控制台应用--输入一个日期,求该日期是这一年中的第几天
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:输入一个日期,求该日期是这一年中的第几天

* 作 者: 雷恒鑫
* 完成日期: 2012 年 09 月 09 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:

* 程序头部的注释结束

*/


using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication_do_while{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("       |----------------------------|");            Console.WriteLine("       |   这是一个“输入一个日期,求该日期是这一年中的第几天 ”的程序  |");            Console.WriteLine("       |----------------------------|");            Console.WriteLine();            Console.WriteLine("您需要输入一个日期...");            Console.WriteLine();            Console.Write("您需要输入年份:");            int year = int.Parse(Console.ReadLine());            Console.WriteLine();            Console.Write("您需要输入月份:");            int month = int.Parse(Console.ReadLine());            Console.WriteLine();            Console.Write("您需要输入具体日期(几号?):");            int date = int.Parse(Console.ReadLine());            Console.WriteLine();            Console.Write("{0}年{1}月{2}日是一年中的第",year,month,date);            bool b = Leap_Common_year(year);//判断平年还是闰年            switch(month)     //判断月份            {                case 1:month_1(date);break;//根据日期计算出是哪一天                case 2:month_2(date);break;                case 3:month_3(date,b);break;                case 4:month_4(date,b);break;                case 5:month_5(date,b);break;                case 6:month_6(date,b);break;                case 7:month_7(date,b);break;                case 8:month_8(date,b);break;                case 9:month_9(date,b);break;                case 10:month_10(date,b);break;                case 11:month_11(date,b);break;                case 12:month_12(date,b);break;                default: Console.WriteLine("您输入的月份有误..."); break;            }            Console.ReadKey();        }        static bool Leap_Common_year(int year)//判断平年还是闰年        {            bool b;            if (year % 4 == 0 && year % 100 != 0)            {                b = false;            }            else            {                b = true;            }            return b;        }        static void month_1(int date)        {            Console.Write("{0}天。",date);        }        static void month_2(int date)        {            int d;            d = 31 + date;            Console.Write("{0}天",d);        }        static void month_3(int date, bool b)        {            int d;            if (b)            {                d = 31 + 28 + date;            }            else            {                d = 31 + 29 + date;            }            Console.Write("{0}天", d);        }        static void month_4(int date, bool b)        {            int d;            if (b)            {                d = 31 + 28 + 31 + date;            }            else            {                d = 31 + 29 + 31 + date;            }            Console.Write("{0}天", d);        }        static void month_5(int date, bool b)        {            int d;            if (b)            {                d = 31 + 28 + 31 + 30 + date;            }            else            {                d = 31 + 29 + 31 + 30 + date;            }            Console.Write("{0}天", d);        }        static void month_6(int date, bool b)        {            int d;            if (b)            {                d = 31 + 28 + 31 + 30 + 31 + date;            }            else            {                d = 31 + 29 + 31 + 30 + 31 + date;            }            Console.Write("{0}天", d);        }        static void month_7(int date, bool b)        {            int d;            if (b)            {                d = 31 + 28 + 31 + 30 + 31 + 30 + date;            }            else            {                d = 31 + 29 + 31 + 30 + 31 + 30 + date;            }            Console.Write("{0}天", d);        }        static void month_8(int date, bool b)        {            int d;            if (b)            {                d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + date;            }            else            {                d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + date;            }            Console.Write("{0}天", d);        }        static void month_9(int date, bool b)        {            int d;            if (b)            {                d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + date;            }            else            {                d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + date;            }            Console.Write("{0}天", d);        }        static void month_10(int date, bool b)        {            int d;            if (b)            {                d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date;            }            else            {                d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date;            }            Console.Write("{0}天", d);        }        static void month_11(int date, bool b)        {            int d;            if (b)            {                d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date;            }            else            {                d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date;            }            Console.Write("{0}天", d);        }        static void month_12(int date, bool b)        {            int d;            if (b)            {                d = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date;            }            else            {                d = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date;            }            Console.Write("{0}天", d);        }    }}               


 

运行结果:

C#第2周实验-任务9-编撰一个控制台应用-输入一个日期,求该日期是这一年中的第几天

 

C#第2周实验-任务9-编撰一个控制台应用-输入一个日期,求该日期是这一年中的第几天

 

问题:我发现了在switch语句里,default后面必须加break,否则程序报错。

这和C++有点不一样。

 

 

热点排行