C#的数组基础知识

1 数组的声明及赋值方法

方式一

  int[] scores = {1,2,3,4,5};//在数组声明的时候赋值

方式二:定义数组的长度,没有赋值,元素默认值为0

   int[] scores = new int[10];//数组长度为10,数组元素默认值是0
       //也可以这样写
    int[] scores;//数组的初始化
    scores = new int[10];

方式三:定义数组的长度,有赋值

 int[] scores =new int[5]{ 1, 2, 3, 4, 5 };//[]里的值可以不指定

注意!!!当访问一个索引不存在的值的时候,出现空指针异常

int[] scores = {1,2,3,4,5};
    Console.WriteLine(scores[10]);//空指针异常报错

下面给一个示例:

        static void Main(string[] args)//数组声明和赋值方法
        {
            //声明
            int[] array;
            //初始化 new 数据类型[容量]
            array = new int[6];
            //通过索引读写每个元素
            array[0] = 1;//将数据1赋值给数组的第1个元素
            array[1] = 2;//将数据2赋值给数组的第2个元素
            array[3] = 3;//将数据3赋值给数组的第4个元素
                         // for (int i = 0; i < 6; i++)
                         //上句简单写法
            for (int i = 0; i < array.Length; i++)
                Console.WriteLine(array[i]);
            Console.ReadKey();
        }

2 几个数组的练习

(1)输入学生成绩并打印

 static void Main(string[] args)//在控制台输入学生数并录入成绩并打印成绩
        {
            float[] scoreArray = CreateScoreArray();
            Console.WriteLine("—————以下为学生成绩—————");
            for (int i = 0; i < scoreArray.Length; i++)
            {
                Console.WriteLine(scoreArray[i].ToString());
            }
            Console.ReadKey();
        }
        static float[] CreateScoreArray()//在控制台中录入学生成绩
        {
            Console.WriteLine("请输入学生人数:");
            int StudentsCount = int.Parse(Console.ReadLine());
            float[] ScoreArray;
            ScoreArray = new float[StudentsCount];

            for (int i = 0; i < ScoreArray.Length;)
            {
                Console.WriteLine("请输入第{0}个学生成绩:", i + 1);
                float score = float.Parse(Console.ReadLine());

                if (score >= 0 && score <= 100)
                    ScoreArray[i++] = score;
                else
                    Console.WriteLine("输入成绩数据有误!新重新输入");
            }
            return ScoreArray;

        }

(2)返回数组中的最大值,如输入学生的成绩,找出最高成绩

  static void Main(string[] args)// 查找数组最大值

        {
            float[] scoreArray = CreateScoreArray();
            Console.WriteLine("—————以下为学生成绩—————");
            for (int i = 0; i < scoreArray.Length; i++)
            {
                Console.WriteLine(scoreArray[i].ToString());
            }
            float max = GetMax(scoreArray);
            Console.WriteLine("成绩最高为:" + max);
            Console.ReadKey();
        }
        private static float GetMax(float[] array)//查找数组的最大值
        {
            float max = array[0];
            for (int i = 1; i < array.Length; i++)
            {
                if (max < array[i])
                    max = array[i];
            }
            return max;
        }

(3)数组的其他写法

  static void Main(string[] args)//数组的其他写法
        {
            //初始化+赋值,下面的可以分开写,先声明,再赋值,分两行string[]array01;array01=new string[2]{"a","b"};,示例如下:
            string[] array01 = new string[2] { "a", "b" };
            //声明+初始化+赋值,不有分开写,示例如下
            bool[] array02 = { true, false, false };
            //float[] temp = new float[3] { 3,4,6};
            //float max = GetMax(temp);
            Console.WriteLine(GetMax(new float[3] { 3, 4, 6 }).ToString());
            Console.ReadKey();
        }

(4)练习根据年月日,计算当天是本年的第几天

        static void Main(string[] args)
        {
            Console.WriteLine("请输入年:");
            int year = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入月:");
            int month = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入日:");
            int day = int.Parse(Console.ReadLine());
            int days = GetDays(year, month, day);
            Console.WriteLine("您输入年月日是当年的第{0}天", days);
            Console.ReadKey();

        }

        private static int GetDays(int year, int month, int day)
        {
            return GetTotalDays(year, month, day);
        }

        private static int GetTotalDays(int year, int month, int day)
        {
            int[] daysOfMonth =  { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
           
            if (IsLeapYear(year) )  daysOfMonth[1] = 29;
            int days = 0;
            for (int i=0;i<(month-1); i++)
            {
                days+=daysOfMonth[i];
            }
            return days+day;
      
        }
        private static bool IsLeapYear(int year)
        {
            return (year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0);
        }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值