类、数组03/12

 类、数组03/12

1、输入身份证号,截取生日

        Console.Write("请输入身份证号:");

            string a = Console.ReadLine();

            if (a.Length == 18)

            {

                string y = a.Substring(6, 4);

                string m = a.Substring(10, 2);

                string d = a.Substring(12, 2);

                Console.WriteLine("你的生日为" + y + "年" + m + "月" + d + "日。");

            }

            else

            {

                Console.WriteLine("你输入的有误!");

            }

2、判断邮箱格式是否正确1.有且只能有一个@;2.不能以@开头;3.@之后至少有一个.;4.@和.不能靠在一起;5.不能以.结尾。

         Console.Write("请输入邮箱:");

            string m = Console.ReadLine();

            bool a = m.Contains("@");

            if (a == true)

            {

                int b = m.IndexOf("@");

                int c = m.LastIndexOf("@");

                if (b == c)

                {

                    if (b != 0)

                    {

                        string m1 = m.Substring(b); //3.@之后至少有一个.

                        if (m1.Contains("."))

                        {

                            int d = m1.IndexOf(".");

                            if (d != 1)

                            {

                                int e = m1.Length;

                                int f = m1.LastIndexOf(".");

                                if (e - 1 != 1)

                                {

                                    Console.WriteLine("你输入的格式正确");

                                }

                                else

                                {

                                    Console.WriteLine("你输入的格式有误");

                                }

                            }

                            else

                            {

                                Console.WriteLine("你输入的格式有误");

                            }

                        }

                        else

                        {

                            Console.WriteLine("你输入的格式有误");

                        }

                    }

                    else

                    {

                        Console.WriteLine("你输入的格式有误");

                    }

                }

                else

                {

                    Console.WriteLine("你输入的格式有误");

                }

            }

            else

            {

                Console.WriteLine("你输入的格式有误");

            }          

       

3、随机数类 Random

 Random ran = new Random();//定义一个叫ran的随机数类,并初始化。

 double a = ran.Next(10);//随机找10以内的数

 Console.WriteLine(a);

随机输入验证码验证码

         string s = "abcdefghijklmnopqistuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

            Random ran = new Random();

            for (; ; )

            {

                string biao = "";

                for (int i = 1; i <= 4; i++)

                {

                    int a = ran.Next(s.Length);

                    string b = s.Substring(a, 1);

                    biao = biao + b;

                }

                Console.WriteLine(biao);

                Console.Write("请输入验证码:");

                string yan = Console.ReadLine();

                if (biao.ToLower() == yan.ToLower())

                {

                    Console.WriteLine("输入正确!");

                    System.Threading.Thread.Sleep(2000);

                    break;

                }

                else

                {

                    Console.Write("你输入的有误.");

                    System.Threading.Thread.Sleep(2000);

                    Console.Clear();

                }

            }

                      数组

数组:具有相同类型的若干变量按有序的形式组织起来的一种形式。这些按序排列的同类数据元素的集合称为数组。

一维数组

int[] 变量名 = new int [n];//定义一个int型一维数组,有n个数据

 

例://第一种定义

            int[] shuzu = new int[5];//5代表个数

            ////索引从0开始

            ////付值

            shuzu[0] = 1;

            shuzu[1] = 2;

            shuzu[2]=3;

            shuzu[3]=4;

            shuzu[4]=5;

            Console.WriteLine(2);  输出结果为3

//第二种定义方式

            int[] shuzu = new int[] { 1, 2, 3, 4, 5 };

            Console.WriteLine(shuzu[2]);

 

二维数组

第一种表示int[,] shuzu = new int[3, 4];

//3个数组,每一个数组有4个数;34

第二种表示int[,] shuzu = new int[,]{

           {1,2,34,5},

           {5,4,7,9},

           {6,7,5,4},

           };

多(三)维数组

            int[, ,] shuzu = new int[3, 2, 4];

            {

                {

                    {,,,},

                    {,,,}

                },

                {

                    {,,,},

                    {,,,}

                },

                {

                    {,,,},

                    {,,,}

                }

            };

 

 

1、从控制台输入10个人的年龄放入数组,将十个人的年龄求总和

       int[] y=new int[10];

            int sum = 0;

            for (int i = 0; i < 10;i++ )

            {

                Console.Write("请输入第"+(i+1)+"个人的年龄:");

                y[i]=int.Parse(Console.ReadLine());

                sum+=y[i];

            }

            Console.WriteLine("年龄总合:"+sum);

2、输入人名放进数组,输出第5个人的姓名 

        Console.Write("请输入人数:");

            int a = int.Parse(Console.ReadLine());           

            string[] n=new string[a];

            for (int i = 0; i < 5;i++ )

            {

                Console.Write("请输入第"+(i+1)+"个人的姓名:");

                n[i] = Console.ReadLine();

            }

            Console.WriteLine(4);

3、输入班级人数,根据人数,挨个输入成绩,求平均分

       Console.Write("请输入班级人数:");

            int a = int.Parse(Console.ReadLine());

            int[] fenshu=new int[a];

            int sum = 0;

            for (int i = 0; i < a;i++ )

            {

                Console.Write("请输入第"+(i+1)+"个人的分数:");

                fenshu[i] = int.Parse(Console.ReadLine());

                sum += fenshu[i];

            }

            Console.WriteLine("平均分为:"+sum/a);

4、输入班级人数,将每个人的语文,数学,英语成绩输入二维数组   

        Console.Write("输入班级人数:");

            int a = int.Parse(Console.ReadLine());

            double[,] fenshu=new double[a,3];

            for (int i = 0; i < a;i++ )

            {

                for (int j = 0; j < 3;j++ )

                {

                    if(j==0)

                    {

                        Console.Write("请输入第"+(i+1)+"个人的语文成绩:");

                    }

                    if (j == 1)

                    {

                        Console.Write("请输入第" + (i + 1) + "个人的数学成绩:");

                    }

                    if (j == 2)

                    {

                        Console.Write("请输入第" + (i + 1) + "个人的英语成绩:");

                    }

                    fenshu[i, j] = double.Parse(Console.ReadLine());

                }

            }

5、打印输出一个汉字

           string[,] zi = new string[9, 9]

            {

{"  ","  ","  ","  ","  ","■","  ","  ","  "},

{"  ","  ","  ","  ","■","  ","  ","  ","  "},

{"  ","  ","  ","■","  ","■","  ","  ","  "},

{"  ","  ","■","  ","  ","  ","■","  ","  "},

{"  ","■","  ","  ","  ","■","  ","■","  "},

{"■","  ","  ","■","  ","■","  ","  ","■"},

{"  ","  ","  ","■","  ","■","■","■","  "},

{"  ","  ","  ","■","  ","■","  ","  ","  "},

{"■","■","■","■","■","■","■","■","■"}

            };

            for (int i = 0; i < 9; i++)

            {

                for (int j = 0; j < 9; j++)

                {

                    Console.Write(zi[i, j]);

                }

                Console.WriteLine();

            }

原文地址:https://www.cnblogs.com/zst062102/p/5270093.html