美文网首页
Lession03 基础语法(2)

Lession03 基础语法(2)

作者: 任人渐疏_Must | 来源:发表于2021-03-16 11:52 被阅读0次

数组

using System;

namespace 数组
{
    /*
     * 数组:一次性存储多个相同类型的变量
     * 语法:
     * A.数组类型[] 数组名 = new 数组类型[数组的长度]
     * B.数组类型[] 数组名 = {值1,值2,值3,.....}
     * C.数组类型[] 数组名 = new 数据类型[数组长度]{值1,值2,值3,....}
     * D.数组类型[] 数组名 = new 数据类型[]{值1,值2,值3,....}
     * Array.Sort() 升序排列
     * Array.Reverse()  数组反转
     * 
     * 
     */
    class Program
    {
        static void Main(string[] args)
        {
            int[] nums = new int[10];
            int[] numTwo = { 1, 2, 3, 4, 5, 6 };
            int[] numThree = new int[3] { 1, 2, 3 };
            int[] numFour = new int[] { 1, 2, 3 };

            //nums[0] = 1;
            //nums[1] = 2;
            //nums[2] = 3;
            //nums[3] = 4;
            //nums[4] = 5;
            //nums[5] = 6;
            //nums[6] = 7;
            //nums[7] = 8;
            //nums[8] = 9;
            //nums[9] = 10;
            for (int i = 0; i < nums.Length; i++)
            {
                nums[i] = i + 1;
            }

            //for (int i = 0; i < nums.Length; i++)
            //{
            //    Console.WriteLine(nums[i]);
            //}
            foreach (int i in nums)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("---------------------------------------------");

            /*1.控制台输入要登记的学生人数
             * 2.循环存入学生姓名
             * 3.打印出学生姓名
             */
            Console.WriteLine("请输入您要登记的学生人数");
            //输入信息并转化为int类型
            int count = int.Parse(Console.ReadLine());
            //声明一个存放姓名的字符串数组,其长度等于提供的学生人数
            string[] names = new string[count];
            //用for 循环来接受姓名
            for(int i = 0; i < count; i++)
            {
                Console.WriteLine("请输入学生{0}的姓名",i+1);
                names[i] = Console.ReadLine();
            }
            Console.WriteLine("已登记的学生如下:");
            //用froeach循环显示姓名
            foreach (string name in names)
            {
                Console.WriteLine("{0}", name);
            }
            Console.WriteLine("---------------------------------------------");
            /*
             * 求数组中元素的最大值和最小值
             * 
             */
            //创建一个长度为5数据类型为int类型的数组
            int[] arr = new int[5];
            //声明一个最大值和最小值的变量
            int max, min;
            Console.WriteLine("请输入五个数字");
            //在arr数组中存放值
            for(int i = 0;i < 5; i++)
            {
                //将字符串转换为int类型
                arr[i] =Convert.ToInt32 (Console.ReadLine());
            }
            //默认数组第一个数字时目前最大值,也是最小值
            max = arr[0];
            min = arr[0];
            foreach(int i in arr)
            {
                if(i > max)
                {
                    max = i;
                }
                if(i < min)
                {
                    min = i;
                }
            }
            Console.WriteLine("最大值是{0},最小值是{1}", max,min);
            Console.WriteLine("----------------------------------------------");

            /*
             * 1.创建长度为5 数据类型是int的数组
             * 2.循环添加学生成绩
             * 3.使用冒泡排序
             * 4.输出成绩
             * 
             */
            int[] scores = new int[5];
            int temp;
            Console.WriteLine("请输入五个学生成绩:");
            for(int i = 0; i < 5; i++)
            {
                Console.WriteLine("请输入第{0}个学生成绩",i+1);
                scores[i] = int.Parse(Console.ReadLine());
            }

            //开始对成绩进行降序排序
            for (int i = 0; i < scores.Length-1;i++)
            {
                for(int j = 0; j < scores.Length - 1 - i; j++)
                {
                    if(scores[j] < scores[j + 1])
                    {
                        temp = scores[j];
                        scores[j] = scores[j + 1];
                        scores[j + 1] = temp;
                    }
                }
            }
            Console.WriteLine("排序后的顺序是:");
            foreach(int i in scores)
            {
                Console.WriteLine(i);
            }


            Console.ReadKey();
        }
    }
}


枚举

using System;

namespace 枚举
{
    /*
         * 语法:
         * [public] enum 枚举名
         * {
         *      值1,
         *      值2,
         *      值3,
         *      ......
         * 
         * }
         * 
         * public:访问修饰符,公开的公共的,哪都可以访问
         * enum:关键字,声明枚举的关键字
         * 枚举名:标识符
         * 枚举就是一个变量类型,只是枚举声明、赋值、使用方式和普通的变量类型不一样
         * 
         * 
         * 
         */

    //将枚举声明到命名空间的下面,类的外面,表示这个命名空间下,所有的类都可以使用这个枚举

    public enum Gender
    {
        男,
        女
    }

    public enum Sesons
    {
        春,
        夏,
        秋,
        冬,
    }
    public enum QQState
    {
        Online,
        OffLine,
        Leave,
        Busy,
        QMe
    }


    class Program
    {
        static void Main(string[] args)
        {
           //变量类型 变量名 = 值
            Gender gender = Gender.男;
            Sesons sesons = Sesons.夏;
            QQState qqstate = QQState.Leave;
            Console.WriteLine(gender);
            Console.WriteLine(sesons);
            Console.WriteLine(qqstate);

            #region 将枚举类型强转成int类型
            QQState q = QQState.Leave;
            //枚举类型默认可以跟int类型互相转换,枚举类型跟int类型是兼容的
            int n = (int)q;
            Console.WriteLine((int)QQState.Online);  //0
            Console.WriteLine((int)QQState.OffLine);  //1
            Console.WriteLine(n);  //2
            Console.WriteLine((int)QQState.Busy);   //3
            Console.WriteLine((int)QQState.QMe);    //4
            Console.ReadKey();
            #endregion

            #region 将int类型强转成枚举类型
            //int n1 = 3;
            //QQState state = (QQState)n1;
            //Console.WriteLine(state); //Busy
            //int n2 = 8;
            //QQState state1 = (QQState)n2;   
            //Console.WriteLine(n2);   //8
            //Console.ReadKey();
            #endregion


            Console.ReadKey();
        }
    }
}


数据类型转换

using System;

namespace 数据类型转换
{
        /*类型转换:
         * 我们要求等号两遍参与运算的操作数的类型必须一致
         * 两种类型兼容
         * 自动类型转换   小->大 int->double
         * 强制类型转换   大->小 double->int
         * 两种类型不兼容时:转换使用 Convert  /    int.Parse(s)
         * string s = "123";
         * double d = Convert.ToDouble(s)
         * int i = Convert.ToInt32(s)/int.Parse(s);
         * string ss = "123abc"; (这种不能转,不是纯数字)
         * double dd = Convert.ToDouble(ss);(这种转换会报异常)
         * 
         */
    class Program
    {
        public enum QQState
        {
            Online,
            OffLine,
            Leave,
            Busy,
            QMe
        }

        static void Main(string[] args)
        {
            //强制类型转换
            double score = 58.5;
            int bonus = 2;
            int sum;
            sum = (int)score + bonus;
            Console.WriteLine(sum);
            Console.WriteLine("--------------------------");

            //所有类型都能转换成string类型   ToString()
            int n1 = 10;
            double n2 = 3.14;
            string s = n1.ToString();
            string s2 = n2.ToString();
            Console.WriteLine(s);
            Console.WriteLine(s2);
            //枚举类型转换成string类型
            QQState state = QQState.Online;
            string s3 = state.ToString();
            Console.WriteLine(s3);
            Console.WriteLine("--------------------------");

            #region 将字符串类型转成枚举类型
            string s4 = "3";
            //将s转换成枚举类型
              //QQState state2 = (QQState)s4;  //不能使用这种方式,不兼容
            // Convert.ToInt32() int.parse()   int.TryParse()
            //调用Parse()的目的就是为了让它帮助我们将一个字符串转换成对应的枚举类型
            //如果转换的字符串是数字,则就算枚举中没有,也不会抛异常
            //如果转换的字符串是文本,如果枚举中没有,则会抛出异常
            QQState state2 =(QQState)Enum.Parse(typeof(QQState), s4);
            Console.WriteLine(state2);
            //Console.ReadKey();
            #endregion

            Console.ReadKey();




        }
    }
}


相关文章

网友评论

      本文标题:Lession03 基础语法(2)

      本文链接:https://www.haomeiwen.com/subject/axtdcltx.html