• 练习:结构体、枚举类型——8月3日


    练习1:将结构体添加到集合中,取出某个索引位置的信息进行查看

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace 练习题
    {
        class Program
        {
            //练习1:结构体
            struct Student
            {
                public int no;
                public string name;
                public string sex;
                public double score;
            }
            
            
            static void Main(string[] args)
            {
                //练习1:将结构体添加到集合中,取出某个索引位置的信息进行查看
                Console.Write("请输入班级人数:");
                int n = int.Parse(Console.ReadLine());
                //定义一个集合,并初始化
                ArrayList al = new ArrayList();
                for (int i = 0; i < n; i++)
                {
                    //初始化结构体,每放一个人的信息时都要进行初始化,所以放在循环内部
                    Student st = new Student();
                    Console.Write("请输入第{0}个人的学号:", (i + 1));
                    st.no = int.Parse(Console.ReadLine());
                    Console.Write("请输入第{0}个人的姓名:", (i + 1));
                    st.name = Console.ReadLine();
                    Console.Write("请输入第{0}个人的性别:", (i + 1));
                    st.sex = Console.ReadLine();
                    Console.Write("请输入第{0}个人的分数:", (i + 1));
                    st.score = double.Parse(Console.ReadLine());
                    al.Add(st);
                }
                //重新定义一个结构体,
                Student st1 = new Student();
                //取出某个索引号位置的信息进行查看            
                Console.WriteLine("按回车键查看第二个学生的信息!");
                Console.ReadLine();
                st1 = (Student)al[1];
                Console.WriteLine("该学生的学号是:{0},姓名是:{1},性别是:{2},分数是:{3}", st1.no, st1.name, st1.sex, st1.score);
                Console.ReadLine();
            }
        }
    }

    练习2:定义一个结构体,里面具有姓名,语数英成绩每个人的信息放入结构体中,将所有人的信息添加进集合中,求语文总分,数学平均分

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace 练习题
    {
        class Program
        {       //练习2:结构体
            struct student
            {
                public string name;
                public double yu;
                public double shu;
                public double ying;
            }
            
            static void Main(string[] args)
            {
                //练习2:定义一个结构体,里面具有姓名,语数英成绩            
                //将,每个人的信息放入结构体中
                //将所有人的信息添加进集合中
                //求语文分数的总分
                //求数学分数的平均分
                //查看英语分数的两个最高分的人的全部信息(姓名、语文成绩,数学成绩,英语成绩)
                //使用冒泡排序,
                Console.Write("请输入班级人数:");
                int n = int.Parse(Console.ReadLine());
                //集合初始化,初始化首先要在上面引用:using System.Collections;
                ArrayList al = new ArrayList();
                double a = 0;
                double b = 0;
                for (int i = 0; i < n; i++)
                {
                    //结构体初始化,每放一个人的信息就要重新初始化
                    student st = new student();
                    Console.Write("请输入第{0}个人的姓名:", (i + 1));
                    st.name = Console.ReadLine();
                    Console.Write("请输入第{0}个人的语文成绩:", (i + 1));
                    st.yu = double.Parse(Console.ReadLine());
                    a += st.yu;
                    Console.Write("请输入第{0}个人的数学成绩:", (i + 1));
                    st.shu = double.Parse(Console.ReadLine());
                    b += st.shu;
                    Console.Write("请输入第{0}个人的英语成绩:", (i + 1));
                    st.ying = double.Parse(Console.ReadLine());
                    al.Add(st);
                }
                Console.WriteLine("所有人的信息输入完毕,请按回车键继续!");
                Console.ReadLine();
                Console.WriteLine("语文成绩的总分数是:" + a);
                Console.WriteLine("数学成绩的平均分数是:" + b / n);
                //冒泡排序,英语两个最高分            
                for (int i = 0; i < n; i++)
                {
                    student s1 = new student();
                    s1 = (student)al[i];
                    for (int j = i + 1; j < n; j++)
                    {
                        student s2 = new student();
                        s2 = (student)al[j];
                        if (s1.ying < s2.ying)
                        {
                            object zhong = al[i];
                            al[i] = al[j];
                            al[j] = zhong;
                        }
                    }
                }
                student st1 = new student();
                st1 = (student)al[0];
                student st2 = new student();
                st2 = (student)al[1];
                Console.WriteLine("英语分数最高的第一个同学是:{0},语文分数为:{1},数学分数为:{2},英语分数为:{3}", st1.name, st1.yu, st1.shu, st1.ying);
                Console.WriteLine("英语分数最高的第二个同学是:{0},语文分数为:{1},数学分数为:{2},英语分数为:{3}", st2.name, st2.yu, st2.shu, st2.ying);
                Console.ReadLine();
                
    
            }
        }
    }

    练习3:选班长

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace 练习题
    {
        class Program
        {
                   
            static void Main(string[] args)
            {            
                //练习3:三个候选班长,张三,李四,王五,20个人投票,
                //1,2,3分别对应张三,李四,王五,其他均视为废票,
                //要求20个人输入的全部放进数组中,查看谁的票数最高
                
                int[] array = new int[20];
                for (int i = 0; i < 20; i++)
                {
                    Console.Write("请输入第{0}个人的投票数:", (i + 1));
                    array[i] = int.Parse(Console.ReadLine());
                }
                int sum1 = 0;
                int sum2 = 0;
                int sum3 = 0;
                int sum4 = 0;
                for (int i = 0; i < 20; i++)
                {
                    if (array[i] == 1)
                    {
                        sum1++;
                    }
                    else if (array[i] == 2)
                    {
                        sum2++;
                    }
                    else if (array[i] == 3)
                    {
                        sum3++;
                    }
                    else
                    {
                        sum4++;
                    }
                }
                Console.WriteLine("作废票数为:" + sum4);
                Console.WriteLine("张三的得票数为:" + sum1);
                Console.WriteLine("李四的得票数为:" + sum2);
                Console.WriteLine("王五的得票数为:" + sum3);
                if (sum1 > sum2 && sum1 > sum3)
                {
                    Console.WriteLine("当选班长的是:张三!");
                }
                else if (sum2 > sum1 && sum2 > sum3)
                {
                    Console.WriteLine("当选班长的是:李四!");
                }
                else if (sum3 > sum1 && sum3 > sum2)
                {
                    Console.WriteLine("当选班长的是:王五!");
                }
                else
                {
                    Console.WriteLine("有相同票数,请重新投票!");
                }
                Console.ReadLine();
                
    
            }
        }
    }

     练习4:购车

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Collections;
    
    namespace 练习_购车
    {
        class Program
        {
            struct car
            {
                public string xh;
                public double jg;
                public double zj;
                public double yh;
            }
            static void Main(string[] args)
            {
                //定义一个结构体,存放关于车辆的几个信息
                //将所有车的信息都放入集合中
                //车型号               价格(W)    轴距 (mm)         油耗(L/100km)
                //宝马320Li           38              2920                      6.9
                //宝马520Li           43              3108                     7.2
                //宝马730Li            89              3210                       6.3
                //奥迪A4L35TFSI   31              2869                        6.2 
                //奥迪A6L30TFSI   43              3012                        7.6
                //奥迪A8L45TFSI   89              3122                        8.1
                //奔驰C200L           35              2920                        6.1
                //奔驰E260L           48              3014                        6.7
                //奔驰S320L           93              3165                        8
                ArrayList al = new ArrayList();
                car c = new car();
                c.xh = "宝马320Li";
                c.jg = 38;
                c.zj = 2920;
                c.yh = 6.9;
                al.Add(c);
                c.xh = "宝马520Li";
                c.jg = 43;
                c.zj = 3108;
                c.yh = 7.2;
                al.Add(c);
                c.xh = "宝马730Li";
                c.jg = 89;
                c.zj = 3210;
                c.yh = 6.3;
                al.Add(c);
                c.xh = "奥迪A4L35TFSI";
                c.jg = 31;
                c.zj = 2869;
                c.yh = 6.2;
                al.Add(c);
                c.xh = "奥迪A6L30TFSI";
                c.jg = 43;
                c.zj = 3012;
                c.yh = 7.6;
                al.Add(c);
                c.xh = "奥迪A8L45TFSI";
                c.jg = 89;
                c.zj = 3122;
                c.yh = 8.1;
                al.Add(c);
                c.xh = "奔驰C200L";
                c.jg = 35;
                c.zj = 2920;
                c.yh = 6.1;
                al.Add(c);
                c.xh = "奔驰E260L";
                c.jg = 48;
                c.zj = 3014;
                c.yh = 6.7;
                al.Add(c);
                c.xh = "奔驰S320L";
                c.jg = 93;
                c.zj = 3165;
                c.yh = 8;
                al.Add(c);
                ////现在想买一辆价格在50万以内的车,请给出车的型号以及价格 
                //for (int i = 0; i < 9; i++)
                //{
                //    c = (car)al[i];//每次进来都要初始化
                //    if (c.jg <= 50)//价格在50万以内的车的型号以及价格
                //    {
                //        Console.WriteLine("价格在50万以内的车有:{0},价格是:{1}万", c.xh, c.jg);
                //    }
                //}    
    
                ////买得起豪车,可是很关心油耗,给查一下油耗最低的三辆车的型号以及油耗情况
                //for (int i = 0; i < 9; i++)//i<9可以写成i<al.Count,
                //{                              
                //    for (int j = i + 1; j < 9; j++)
                //    {
                //        car c1 = new car();//可省略
                //        c1 = (car)al[i];//比较后重新交换需要重新获取,走一遍外循环,内循环走一整遍,所以要放在内循环内重新获取值
                //        car c2 = new car();//可省略
                //        c2 = (car)al[j];
                //        if (c1.yh > c2.yh)//从小到大排列
                //        {
                //            object zhong = al[i];
                //            al[i] = al[j];
                //            al[j] = zhong;
                //        }
                //    }
                //}
                //car c3 = new car();
                //c3 = (car)al[0];
                //car c4 = new car();
                //c4 = (car)al[1];
                //car c5 = new car();
                //c5 = (car)al[2];
                //Console.WriteLine("油耗最低的三辆车是:" + c3.xh + ",油耗" + c3.yh + "	" + c4.xh + ",油耗" + c4.yh + "	" + c5.xh + ",油耗" + c5.yh);
                ////用for循环
                //for (int i = 0; i < 3; i++)
                //{
                //    car c1 = (car)al[i];
                //    Console.WriteLine("油耗最低的三辆车是:" + c1.xh + ",油耗" + c1.yh);
                //}
    
    
                ////买了车之后后座会经常坐人,所以我现在想要轴距大一些的车,前三名即可,要列出车的型号以及车的轴距
                //for (int i = 0; i < 9; i++)
                //{
                //    for (int j = i + 1; j < 9; j++)
                //    {
                //        car c1 = new car();
                //        c1 = (car)al[i];//比较后重新交换需要重新获取,走一遍外循环,内循环走一整遍,所以要放在内循环内重新获取值
                //        car c2 = new car();
                //        c2 = (car)al[j];
                //        if (c1.zj < c2.zj)//从大到小排列
                //        {
                //            object zhong = al[i];
                //            al[i] = al[j];
                //            al[j] = zhong;
                //        }
                //    }
                //}
                //car c3 = new car();
                //c3 = (car)al[0];
                //car c4 = new car();
                //c4 = (car)al[1];
                //car c5 = new car();
                //c5 = (car)al[2];
                //Console.WriteLine("轴距最大的三辆车是:" + c3.xh + ",轴距" + c3.zj + "	" + c4.xh + ",轴距" + c4.zj + "	" + c5.xh + ",轴距" + c5.zj);
    
                ////有钱就是任性,就买最贵的,不买最好的,直接调出来最贵的车的所有信息
                //for (int i = 0; i < 9; i++)
                //{
                //    for (int j = i + 1; j < 9; j++)
                //    {
                //        car c1 = new car();
                //        c1 = (car)al[i];//比较后重新交换需要重新获取,走一遍外循环,内循环走一整遍,所以要放在内循环内重新获取值
                //        car c2 = new car();
                //        c2 = (car)al[j];
                //        if (c1.jg < c2.jg)//从大到小排列
                //        {
                //            object zhong = al[i];
                //            al[i] = al[j];
                //            al[j] = zhong;
                //        }
                //    }
                //}
                //car c3 = new car();
                //c3 = (car)al[0];
                //Console.WriteLine("最贵的一辆车是:{0},价格是{1},轴距是{2},油耗是{3}。",c3.xh,c3.jg,c3.zj,c3.yh);
                ////方法二:
                //double aa = 0;
                //for (int i = 0; i < al.Count; i++)
                //{
                //    car a = (car)al[i];
                //    if (aa < a.jg)
                //    {
                //        aa = a.jg;
                //    }
                //}
                //for (int i = 0; i < al.Count; i++)
                //{
                //    car a = (car)al[i];
                //    if (aa == a.jg)
                //    {
                //        Console.WriteLine("最贵的一辆车是:{0},价格是{1},轴距是{2},油耗是{3}。", a.xh, a.jg, a.zj, a.yh);
                //    }
                //}
    
                ////本店有哪几款宝马车??列出来所有信息
                //for (int i = 0; i < 9; i++)
                //{
                //    c = (car)al[i];
                //    if (c.xh.Contains("宝马") == true)
                //    {
                //        Console.WriteLine("本店的宝马车有:{0},价格是{1},轴距是{2},油耗是{3}。",c.xh,c.jg,c.zj,c.yh);
                //    }
                //}
    
                ////本店奔驰车里面油耗最低的是哪一个?所有信息
                //for (int i = 0; i < 9; i++)
                //{
                //    for (int j = i + 1; j < 9; j++)
                //    {
                //        car c1 = new car();
                //        c1 = (car)al[i];//比较后重新交换需要重新获取,走一遍外循环,内循环走一整遍,所以要放在内循环内重新获取值
                //        car c2 = new car();
                //        c2 = (car)al[j];
                //        if (c1.yh > c2.yh)//从小到大排列
                //        {
                //            object zhong = al[i];
                //            al[i] = al[j];
                //            al[j] = zhong;
                //        }
                //    }
                //}
                //car c1 = new car();
                //for (int i = 0; i < 9; i++)
                //{
                //    c1 = (car)al[i];
                //    if (c1.xh.Contains("奔驰"))
                //    {
                //        Console.WriteLine("奔驰车中油耗最低的一辆车是:{0},价格是{1},轴距是{2},油耗是{3}。", c1.xh, c1.jg, c1.zj, c1.yh);
                //    }
                //}
    
                ////本店在售车辆中轴距最长的是不是奥迪??
                //for (int i = 0; i < 9; i++)
                //{
                //    for (int j = i + 1; j < 9; j++)
                //    {
                //        car c1 = new car();
                //        c1 = (car)al[i];//比较后重新交换需要重新获取,走一遍外循环,内循环走一整遍,所以要放在内循环内重新获取值
                //        car c2 = new car();
                //        c2 = (car)al[j];
                //        if (c1.zj < c2.zj)//从大到小排列
                //        {
                //            object zhong = al[i];
                //            al[i] = al[j];
                //            al[j] = zhong;
                //        }
                //    }                
                //}
                //car c3 = new car();
                //c3 = (car)al[0];
                //if (c3.xh.Contains("奥迪"))
                //{
                //    Console.WriteLine("本店在售车辆中轴距最长的是奥迪。");
                //}
                //else
                //{
                //    Console.WriteLine("轴距最长的不是奥迪。是{0}",c3.xh);
                //}
    
                ////本店在售车辆轴距最长的是不是油耗最高的??
                ////轴距排序
                //for (int i = 0; i < 9; i++)
                //{
                //    for (int j = i + 1; j < 9; j++)
                //    {
                //        car c1 = new car();
                //        c1 = (car)al[i];//比较后重新交换需要重新获取,走一遍外循环,内循环走一整遍,所以要放在内循环内重新获取值
                //        car c2 = new car();
                //        c2 = (car)al[j];
                //        if (c1.zj < c2.zj)//从大到小排列
                //        {
                //            object zhong = al[i];
                //            al[i] = al[j];
                //            al[j] = zhong;
                //        }
                //    }
                //}
                //car c3 = new car();
                //c3=(car)al[0];
                ////油耗排序
                //for (int i = 0; i < 9; i++)
                //{
                //    for (int j = i + 1; j < 9; j++)
                //    {
                //        car c1 = new car();
                //        c1 = (car)al[i];//比较后重新交换需要重新获取,走一遍外循环,内循环走一整遍,所以要放在内循环内重新获取值
                //        car c2 = new car();
                //        c2 = (car)al[j];
                //        if (c1.yh < c2.yh)//从大到小排列
                //        {
                //            object zhong = al[i];
                //            al[i] = al[j];
                //            al[j] = zhong;
                //        }
                //    }
                //}
                //car c4 = new car();
                //c4 = (car)al[0];
                //if (c4.xh == c3.xh)
                //{
                //    Console.WriteLine("本店在售车辆中轴距最长的也是油耗最大的,型号是:{0}.", c3.xh);
                //}
                //else
                //{
                //    Console.WriteLine("本店在售车辆中轴距最长的不是油耗最大的.");
                //}
    
                ////奔驰车中轴距最长的是哪一辆??
                ////方法一:先排序在筛选
                //for (int j = 0; j < 9; j++)
                //{
                //    for (int k = j + 1; k < 9; k++)
                //    {
                //        car c1 = new car();
                //        c1 = (car)al[j];
                //        car c2 = new car();
                //        c2 = (car)al[k];
                //        if (c1.zj < c2.zj)//从大到小排列
                //        {
                //            object zhong = al[j];
                //            al[j] = al[k];
                //            al[k] = zhong;
                //        }
                //    }
                //}
                ////排完序进行for循环,当第一次出现包含“奔驰”出现,立即跳出,则是奔驰中轴距最长的
                //car c3=new car;
                //for (int i = 0; i < 9; i++)
                //{
                //    c3=(car)al[i];
                //    if (c.xh.Contains("奔驰"))
                //    {
                //        break;//break 跳出最近花括号   continue  跳出本次循环,继续下次循环
                //    }
                //}           
                //Console.WriteLine("奔驰车中轴距最长的一辆是:{0}", c.xh);
                ////方法二:先筛选,再建一个新集合,把筛选出来的车放到一个集合再排序
                //ArrayList al1 = new ArrayList();
                //for (int i = 0; i < 9; i++)
                //{
                //    car a = (car)al[i];
                //    if (a.xh.Contains("奔驰"))
                //    {
                //        al1.Add(a);
                //    }
                //}
                //for (int i = 0; i < al1.Count; i++)
                //{
                //    for (int j = i+1; j < al1.Count; j++)
                //    {
                //        car c1 = (car)al1[i];
                //        car c2 = (car)al1[j];
                //        if (c1.zj < c2.zj)
                //        {
                //            object zhong = al1[i];
                //            al1[i] = al1[j];
                //            al1[j] = zhong;
                //        }
                //    }
                //}
                //car c3 = (car)al1[0];
                //Console.WriteLine("奔驰车中轴距最长的一辆是:{0}", c3.xh);
    
                ////就只喜欢宝马,可是没多少钱,请列出来现有车辆中最便宜的宝马车的所有信息
                //for (int i = 0; i < 9; i++)
                //{ 
                //    for(int j = i+1; j < 9; j++)
                //    {
                //        car c2=new car();
                //        c2=(car)al[i];
                //        car c3=new car();
                //        c3=(car)al[j];
                //        if(c2.jg > c3.jg)//从小到大
                //        {
                //            object zhong=al[i];
                //            al[i]=al[j];
                //            al[j]=zhong;
                //        }
                //    }
                //}
                ////排完序进行for循环,当第一次出现包含“宝马”出现,立即跳出,则是宝马中价格最低的
                ////筛选时要重新初始化
                //car c1 = new car();
                //for(int i = 0; i < 9; i++)
                //{               
                //    c1=(car)al[i];
                //    if (c1.xh.Contains("宝马"))
                //    {
                //        Console.WriteLine("宝马车中价格最低的一辆是:{0},价格是{1}万,轴距是{2},油耗是{3}。", c1.xh,c1.jg,c1.zj,c1.yh);
                //    }
                //}
    
                Console.ReadLine();
    
            }
        }
    }
  • 相关阅读:
    docker镜像
    docker常用命令
    docker基础
    跨站脚本漏洞(XSS)基础
    Session、Cookie与Token
    linux之curl工具
    ssl证书与java keytool工具
    mysql主从复制
    linux之平均负载(学习笔记非原创)
    mysql8.0忘记密码如何操作?
  • 原文地址:https://www.cnblogs.com/juyangchao12/p/5734479.html
Copyright © 2020-2023  润新知