码迷,mamicode.com
首页 > 编程语言 > 详细

_14__enum星期几

时间:2015-02-11 10:33:05      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

class EnumApp
{
    // 声明一个枚举类型Weekday,基类为int,访问范围为public
    public enum Weekday
    {
        Sun, Mon, Tue, Wed, Thu, Fri, Sat
    }
    
    public static void Main()
    {
        // 用枚举类型Weekday声明变量并赋初值
        Weekday wd1 = Weekday.Mon;
        Console.WriteLine("每周的工作日从{0}开始",wd1);
        Weekday wd2=Weekday.Fri;
        Console.WriteLine("到{0}结束",wd2);
        
        // 枚举类型变量用于计算
        Console.WriteLine("每周有{0}个工作日", wd2-wd1);
        Console.WriteLine();

        // 枚举类型变量作为函数的参数
        Console.WriteLine("Mon之后是{0}",NextDay(Weekday.Mon));
        Console.WriteLine();
        
        // 使用枚举类型的GetNames()方法
        foreach(string s1 in Weekday.GetNames(typeof(Weekday)))
        {
            Console.Write(s1+" ");
        }
        Console.WriteLine("\n");
        
        // 从命令行读取枚举类型变量的值
        Console.Write("今天是星期几? ");
        Weekday wd3=(Weekday)Weekday.Parse(typeof(Weekday),Console.ReadLine());
        Console.WriteLine("明天是{0}",NextDay(wd3));
        Console.WriteLine();
        
        // 比较枚举类型的值
        Console.Write("体育课在星期几? ");
        wd1=(Weekday)Weekday.Parse(typeof(Weekday),Console.ReadLine());
        Console.Write("物理试验在星期几? ");
        wd2=(Weekday)Weekday.Parse(typeof(Weekday),Console.ReadLine());
        if(wd1.CompareTo(wd2) == 0)
        {
            Console.WriteLine("糟糕,在同一天。");
        }
        else
        {
            Console.WriteLine("没事,不在同一天");
        }
    }
    
    // 功能: 得到某日的下一日(星期几)
    // 参数:
    //        wd : 枚举类型Weekday
    // 返回值: 枚举类型Weekday
    public static Weekday NextDay(Weekday wd)
    {
        Weekday wdnext;
        if (wd == Weekday.Sat)
        {
            wdnext=Weekday.Sun;
        }
        else
        {
            wdnext=wd+1;
        }

        return wdnext;
    }
}

 

_14__enum星期几

标签:

原文地址:http://www.cnblogs.com/xmcrew/p/4285335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!