C# 面对对象基础 枚举,Enum.TryParse的使用

代码:

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Student_c_
{
    enum Week : int
    {
        Mon,
        Tus,
        Wed,
        Thu,
        Fri,
        Sat,
        Sun,
    }

    public class Student
    {

        public static string Course = "面向对象程序设计!";
        public static string Where = "G2-407";
        public string Name { get; set; }
        public string ClassName { get; set; }
        private int Id;
        // 班级的最大容量,这是一个常量  
        public const int MaxNum = 39;
        public bool flag = true;
        // 学生的ID  
        public int id
        {
            get
            {
                return Id;
            }
            set
            {
                if(value >MaxNum||value <0)
                {
                    Console.WriteLine("学号输入错误");
                    flag = !flag;
                }
                else
                {
                    Id = value;
                }
            }
        }

        // 构造函数  
        public Student( int id,string name, string className)
        {
            this.id=id;
            Name = name;
            ClassName = className;
        }

        internal class Program
        {
            public static void GotoClass(Student stu)
            {
                if(stu.flag)
                {
                    Console.WriteLine("学号是:{2} 班级是:{0} 姓名是:{1}", stu.ClassName, stu.Name, stu.Id);
                }
                else
                {
                    Console.WriteLine(" 班级是:{0} 姓名是:{1}", stu.ClassName, stu.Name);
                }
               
            }
            static void Main(string[] args)
            {
                string a, b;
                int c;
                string day;
                Console.WriteLine("输入学号:");
                c = int.Parse(Console.ReadLine());
                Console.WriteLine("输入班级:");
                a = Console.ReadLine();
                Console.WriteLine("输入姓名:");
                b = Console.ReadLine();
                Console.WriteLine("今天是星期几:");
                day = Console.ReadLine();
                Console.WriteLine("\n");
             //   Week worday=(Week)Enum.Parse(typeof(Week), day);
                if (Enum.TryParse<Week>(day, true, out Week workday))//
                {
                    if (workday == Week.Sat||workday==Week.Sun)
                    {
                        Console.WriteLine("Today is weekend!!!");
                    }
                    else
                    {
                        Console.WriteLine("Today is {0}", day);
                    }
                }

                    Student stu = new Student(c, b, a);
                GotoClass(stu);
                Console.WriteLine($"课程是:{Course} 教室是:{Where}");
                Console.ReadLine();
            }
        }
    }
}

注意:这里的对应关系

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值