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);
        }
    }
}
           
    


 

运行结果:

 

 

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

这和C++有点不一样。

 

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leihengxin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值