C#之小练⑧(建立一个类数组,随机抽取数组中的类,并调用对应类的方法;向集合中随机添加数据并求和)

 ①、建立一个类数组,随机抽取数组中的类,并调用对应类的方法

using System;

namespace 类数组
{
    class Program
    {//建立一个类数组,随机抽取数组中的类,并调用对应类的方法
        static void Main(string[] args)
        {
            Person[] per = new Person[10];                  //定义一个Person类数组
            Random r = new Random();                        //定义一个随机数

            for (int i = 0; i < per.Length; i++)            //数组长度为10,循环10次
            {
                int num = r.Next(1, 7);                     //调用随机数r的next方法,确定要取的随机数
                switch (num)                                //随机数有6个,必定会有重复
                {
                    case 1: per[i] = new Person(); break;   //如果num=1,那么实例化类数组per[i]为new Person(),break跳出for 循环
                    case 2: per[i] = new Teacher(); break;  //如果num=2,那么实例化类组per[i]为new Teacher(),break跳出for循环
                    case 3: per[i] = new Student(); break;
                    case 4: per[i] = new ShuaiGuo(); break;
                    case 5: per[i] = new MeiLv(); break;
                    case 6: per[i] = new YeShou(); break;
                }
            }
            for (int i = 0; i < per.Length ; i++)           //循环per数组的长度10次
            {
                ShowType(per[i]);                           //调用ShowType()方法,将per[i]传递给ShowType()方法
            }
            Console.ReadKey();
        }

                                                            //判断拿过来的类转换的是什么类
        public static void ShowType(Person p)
        {
            
            if (p is Teacher)                               //如果p可以转换为Teacher类,则执行大括号内代码
            {
                ((Teacher)p).SayTeacher();                  //p强制转换为Teacher,然后调用Teacher类方法
            }
            else if (p is Student)                          //如果 p转换为Student类为true,则执行大括号内代码
            {
                ((Student)p).SayPerson();                   //p强制转换为Student,然后调用Student类方法     
            }
            else if (p is ShuaiGuo)
            {
                ((ShuaiGuo)p).SayShuaiGuo();
            }
            else if (p is MeiLv)
            {
                ((MeiLv)p).SayMeiLv();
            }
            else if (p is YeShou)
            {
                ((YeShou)p).SayYeShou();
            }
            else if (p is Person)                           //之前是把这个放到了第一个,导致都是调用的Person方法,因为所有的子类都可以转换为父类,所以要把这个放到最后来判断
            {
                ((Person)p).SayPerson();                    //等于Person p=(Person)p ;p.Say()
            }
        }
    }
    public class Person
    {
        string _name;
        public string Name
        {
            get { return _name; }
        }
        char _sex;
        public char Sex
        {
            get { return _sex; }
        }
        int _age;
        public int Age
        {
            get { return _age; }
        }
        public void SayPerson()
        {
            Console.WriteLine("我是{0},{1},今年{2}岁",Name,Sex,Age);
        }
    }
    public class Teacher : Person
    {
        int _salary;
        public int Salary
        {
            get { return _salary; }
        }
        public void SayTeacher()
        {
            Console.WriteLine("我是老师");
        }
    }
    public class Student : Person
    {
        string _id;
        public string Id
        {
            get { return _id; }
        }
        public void SayStudent()
        {
            Console.WriteLine("我是学生");
        }
    }
    public class ShuaiGuo : Person
    {
        public void SayShuaiGuo()
        {
            Console.WriteLine("我是帅哥");
        }
    }
    public class MeiLv : Person
    {
        public void SayMeiLv()
        {
            Console.WriteLine("我是美女");
        }
    }
    public class YeShou : Person
    {
        public void SayYeShou()
        {
            Console.WriteLine("我是野兽");
        }
    }
}

②、向集合中随机添加数据并求和

namespace 向集合中随机添加数据求和
{
    class Program
    {
        static void Main(string[] args)
        {                                                                  //随机的往集合中添加数据,添加10个,不能重复,再求和,求平均数
                                                                           //我的逻辑:216毫秒
            ArrayList al = new ArrayList();                                //实例化一个集合,实例化名称为al
            Random r = new Random();                                       //实例化一个随机数,实例化名称为r
            int sum = 0;
            int num = 0;
            for (int i = 0; i < 100; i++)                                  //遍历1-100
            {
                int rnd = r.Next(1, 100);                                  //声明一个整数rnd来承接随机数r
                if (!al.Contains(rnd))                                     //如果集合al中不包含rnd这个随机数,那么就执行大括号内容,否则重新循环
                {
                    al.Add(rnd);                                           //向集合al中添加rnd
                    sum += rnd;                                            //集合的和等sum=sum+rnd
                    Console.WriteLine("随机数为{0}", rnd);                 //在控制台上写入产生的随机数
                    num += 1;                                              //每循环一次num加1
                    if (num == 10)                                         //如果循环了10次,那么则执行break语句,跳出循环
                    {
                        break;
                    }
                }
            }
            double avg = sum * 1.0 / al.Count;                             //平均数等于总和除以集合的个数
            Console.WriteLine("和为:{0},平均数为:{1}", sum, avg);
            Console.ReadKey();
                                                                           //老师的逻辑:
            ArrayList arr = new ArrayList();
            Random rr = new Random();
                                                                           //向集合中添加不重复的10个数
            while (arr.Count != 10)                                        //While循环,当集合arr总数不为10时,循环大括号内代码
            {
                int numm = rr.Next(1, 100);                                //定义num变量承接随机数
                if (!arr.Contains(numm))                                   //如果arr集合中包含num数据,则不执行集合添加代码
                {
                    arr.Add(numm);                                         //向集合中添加num随机数
                }
            }
                                                                           //计算集合的总和及平均数
            int summ = 0;
            for (int i = 0; i < arr.Count; i++)                            //遍历从0-集合个数
            {
                if (arr[i] is int)                                         //判断是否可以转换
                {
                    summ += (int)arr[i];                                   //如果可以则强制准换为int类型,然后进行计算
                }
                Console.WriteLine("随机数为:{0}",arr[i]);                 //在控制台上写入随机数
            }
            Console.WriteLine("总和为:{0}",summ);                          //在控制台上写入总和
            Console.WriteLine("平均数为:{0}",summ / arr.Count);           //在控制台上写入平均数
            Console.ReadKey();    
        }
    }

代码的点滴积累,下一站走起^_^

猜你喜欢

转载自blog.csdn.net/Elsa15/article/details/86799200
今日推荐