第五讲 练习代码

本文详细介绍了C#中的预定义值类型,包括布尔、字符、整型和浮点数,并展示了它们的使用示例。此外,还讲解了用户自定义类型,如枚举和结构的创建及应用,包括枚举类型的定义和结构类型的实例化。通过具体的代码示例,演示了如何使用结构和枚举来表示和操作数据。
摘要由CSDN通过智能技术生成

数据类型详解

——预定义值类型与用户定义值类型

 

namespace ConsoleApp3

{

    class Program

    {

        static void Main(string[] args)

        {

            //1.布尔bool -> System.boolean

 

            //double score = 75.5;

            //bool passing = false;

            //Console.WriteLine(passing);

            //passing = score > 60;

            //Console.WriteLine(passing);

            //Console.ReadLine();

 

            //2.字符型 char->System.Char

            //char c = 'e';

            //Console.WriteLine(c);

            //Console.WriteLine((int)c);

            //Console.ReadLine();

 

            //char c1 = '\x004b';

            //Console.WriteLine(c1);

            //Console.WriteLine((int)c1);

            //Console.ReadLine();

 

            //short x = 75;

            //char y = (char)x;

            //Console.WriteLine(y);

            //Console.ReadLine();

 

            //3.整型 int ->System.Int32

            //int x = -50;

            //int y = Int32.MaxValue;

            //Console.WriteLine(y);

            //Console.ReadLine();

            //uint z = -5555;

 

            //4.浮点数  

            float f = 3.14f;

            double d = 3.14;

            decimal dec = 3.14m;

            Console.WriteLine(f);

            Console.WriteLine(d);

            Console.WriteLine(dec);

            Console.ReadLine();

 

            decimal dec1 = Decimal.Add(dec, dec);

            Console.WriteLine(dec1);

            Console.ReadLine();

 

            //用户自定义值类型

            //1.枚举

            string name = "张三";

            int age = 20;

            //string gender = "男";

            Gender gender = Gender.男;

            Department dep = Department.营业部;

            Console.WriteLine("姓名:{0},年龄:{1},性别:{2},部门:{3}", name, age, gender,dep);

            Console.ReadLine();

 

            //2.结构

            Department_1 department = new Department_1();

            department.name = "市场部";

            department.tel = "010-12345678";

            department.leader = "李四";

            Console.WriteLine("姓名:{0},年龄:{1},性别:{2},部门:{3}", name, age, gender, department.name);

            Console.ReadLine();

 

            //3.练习:

            //(1)要求打印出下信息“这本书的名字是:C#程序设计,作者是:ROSE,作者性别是:女,售价是:56.8,出版社是:人民邮电出版社.”

            //要求用到“结构”和“枚举”

            BookInfo book = new BookInfo();

            book.bookName = "C#程序设计";

            book.bookAuthor = "ROSE";

            book.authorGender = Gender.女;

            book.bookPrice = 56.8m;

            book.pCompany = "人民邮电出版社";

            Console.WriteLine("这本书的名字是:{0},作者是:{1},作者性别是:{2},售价是:{3},出版社是:{4}.", book.bookName,book.bookAuthor,book.authorGender,book.bookPrice,book.pCompany);

            Console.ReadLine();

 

            //(2)要求打印出下信息“学生信息如下,姓名:MIKE, 年龄:16,性别:男,身高:180cm,数学:135,语文:118,英语:130,C语言:94,总分:477”

            //要求用到“结构”和“枚举”

            Student stu = new Student();

            stu.name = "MIKE";

            stu.age = 17;

            stu.stuGender = Gender.男;

            stu.stature = 180;

            stu.china = 118;

            stu.maths = 135;

            stu.english = 130;

            stu.c = 94;

            int total = stu.c + stu.china + stu.english + stu.maths;

            Console.WriteLine("学生信息如下,姓名:{0}, 年龄:{1},性别:{2},身高:{3}cm,数学:{4},语文:{5},英语:{6},C语言:{7},总分:{8}。",stu.name,stu.age,stu.stuGender,stu.stature,stu.maths,stu.china,stu.english,stu.c,total);

            Console.ReadLine();

        }

    }

    //public enum Gender

    //{

    //    男,

    //    女

    //}

    public enum Gender

    {

        男=1,

        女=2

    }

    public enum Department

    {

        市场部,

        财务部,

        行政部,

        营业部

    }

    public struct Department_1

    {

        public string name;

        public string tel;

        public string leader;

    }

 

    public struct BookInfo

    {

        public string bookName;

        public string bookAuthor;

        public Gender authorGender;

        public decimal bookPrice;

        public string pCompany;

    }

    public struct Student

    {

        public string name;

        public int age;

        public Gender stuGender;

        public int stature;

        public int china;

        public int maths;

        public int english;

        public int c;

    }   

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

海棠弯弯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值