zoukankan      html  css  js  c++  java
  • 传值、传址 结构体

    一、 传值:将变量名中存放的值进行传输
        传址:将这个变量名直接传输过去,
        若在另一边有赋值情况,这边的值会发生变化
     传值
            public void Hanshu(int a)
            {
                a += 10;
                
            }
    只是定义了个变量 
     public void hanshu1(int a , out int b,out int c)
            {
                
                b = a + 10;
                c = b;
                
            }
    在主函数中,先实例化
                     int a = 5;
                int rr;
                int tt;
                hanshu.hanshu1(a, out rr, out tt);
                Console.WriteLine(rr);
                Console.WriteLine(tt);
    可得:rr=tt=15
     public string Fanhui(string name , string sex,int age)
            {
                age += 10;
                return name + "-"+sex +"-"+ age;
            }
      string s = hanshu.Fanhui("张三", "男", 33);
                string[] aa = s.Split('-');
                foreach (string q in aa)
                {
                    Console.WriteLine(q);
                }
    此法可得多个参数,比较常用。
    二、结构体
            结构体:自定义类型    值类型
            一组变量的组合
            需要定义的位置   class里面   main函数外面
            里面包含的变量可以是多种数据类型的
     
            //学生信息的结构体:学号、姓名、性别、分数
            struct Student
            {
                public int xuehao;
                public string name;
                public string sex;
                public Score score;  ---这里声明了一个Score类型的score,Score 则是下面定义的另一个结构体类型
    
            }
     
            struct Score    这里定义了一个名为Score 的结构体
            {
                public double yufen;
                public double shufen;
                public double yingfen;
            }
    例题:
    超市问题
    struct Shop
            {
                public string name;
                public double danjia;
                public int shul;            
            }
                Console.WriteLine("0.开始购买");
                Console.WriteLine("6.结算(退出)");
                Console.Write("请输入您所选选项的代码:");
                for (int i = 0; i == 0; )
                {
                    int a = int.Parse(Console.ReadLine());
                    if (a == 0)
                    {
                        int biao1 = 0;
                        ArrayList al = new ArrayList();
                        for (int j = 0; j == 0; )
                        {
                            Shop sp = new Shop();
                            bool biaocuo = true;
                            Console.Clear();
                            if (al.Count > 0)
                            {
                                for (int k = 0; k < al.Count; k++)
                                {
                                    Shop yigou = (Shop)al[k];
                                    Console.WriteLine("您选择了{0},单价为{1}元,数量为{2}。", yigou.name, yigou.danjia, yigou.shul);
                                }
                            }
                            Console.WriteLine("1.洗发水      15元");
                            Console.WriteLine("2.牙刷        5元");
                            Console.WriteLine("3.可口可乐    3元");
                            Console.WriteLine("4.水杯        12元");
                            Console.WriteLine("5.毛巾        6元");
                            Console.WriteLine("6.结算");
                            Console.Write("请输入您所选选项的代码:");
                            int aa = int.Parse(Console.ReadLine());
                            switch (aa)
                            {
                                case 1:
                                    sp.name = "洗发水";
                                    sp.danjia = 15;
                                    biao1++;
                                    Console.Write("请输入您所选商品的数量:");
                                    break;
                                case 2:
                                    sp.name = "牙刷";
                                    sp.danjia = 5;
                                    biao1++;
                                    Console.Write("请输入您所选商品的数量:");
                                    break;
                                case 3:
                                    sp.name = "可口可乐";
                                    sp.danjia = 3;
                                    biao1++;
                                    Console.Write("请输入您所选商品的数量:");
                                    break;
                                case 4:
                                    sp.name = "水杯";
                                    sp.danjia = 12;
                                    biao1++;
                                    Console.Write("请输入您所选商品的数量:");
                                    break;
                                case 5:
                                    sp.name = "毛巾";
                                    sp.danjia = 6;
                                    biao1++;
                                    Console.Write("请输入您所选商品的数量:");
                                    break;
                                case 6:
                                    if (biao1 == 0)
                                    {
                                        Console.Clear();
                                        Console.WriteLine("您什么也没有购买,您已经走出了超市。。。");
                                        j = 1;
                                        i = 1;
                                    }
                                    else
                                    {
                                        double zong = 0;
                                        for (int k = 0; k < al.Count; k++)
                                        {
                                            Shop yigou = (Shop)al[k];
                                            Console.WriteLine("您选择了{0},单价为{1}元,数量为{2},单品总价:{3}。", yigou.name, yigou.danjia, yigou.shul, yigou.danjia * yigou.shul);
                                            zong += yigou.danjia * yigou.shul;
                                        }
                                        Console.Write("总价:" + zong + "元。请缴费:");
                                        int erjiao = 0;
                                        for (int l = 0; l == 0; )
                                        {
                                            int jiao = int.Parse(Console.ReadLine());
                                            jiao += erjiao;
                                            if (jiao >= zong)
                                            {
                                                Console.WriteLine("交易成功,交易时间为:" + DateTime.Now);
                                                Console.WriteLine("找零:" + (jiao - zong) + "元。");
                                                Console.WriteLine("谢谢惠顾!再见!");
                                                l = 1;
                                                j = 1;
                                                i = 1;
                                            }
                                            else
                                            {
                                                erjiao = jiao;
                                                Console.Write("缴费金额不足,请继续缴费:");
                                            }
                                        }
                                    }
                                    break;
                                default:
                                    Console.WriteLine("查无此商品!请按回车键继续选择商品!");
                                    Console.ReadLine();
                                    biaocuo = false;
                                    break;
                            }
                            if (i == 0 && j == 0 && biaocuo == true)
                            {
                                sp.shul = int.Parse(Console.ReadLine());
                                Console.WriteLine("您选择了{0},单价为{1}元,数量为{2}。请按回车键继续购买!", sp.name, sp.danjia, sp.shul);
                                al.Add(sp);
                                Console.ReadLine();
                            }
                        }
                    }
                    else if (a == 6)
                    {
                        Console.Clear();
                        Console.WriteLine("您没有选择商品,直接退出本超市!");
                        i = 1;
                    }
                    else
                    {
                        Console.WriteLine("输入错误!");
                    }
                }
    超市
    请输入班级人数,输入每个人的学号,姓名,和语文分数、数学分数和英语分数(要求使用结构体),求班级里两个语文分数是最高分的学生的所有信息;数学分数是最高分的两个学生的所有信息;英语平均分。
                struct Student
            {
                public int xuehao;
                public string name;            
                public Score score;
            }
            struct Score
            {
                public double yufen;
                public double shufen;
                public double yingfen;
            }
                ArrayList al = new ArrayList();
                Console.Write("请输入班级人数:");
                int a = int.Parse(Console.ReadLine());
                for (int i = 0; i < a; i++)
                {
                    Student st = new Student();
                    Console.Write("请输入第{0}个学生的学号:", (i + 1));
                    st.xuehao = int.Parse(Console.ReadLine());
                    Console.Write("请输入第{0}个学生的姓名:", (i + 1));
                    st.name = Console.ReadLine();
                    Console.Write("请输入第{0}个学生的语文分数:", (i + 1));
                    st.score.yufen = double.Parse(Console.ReadLine());
                    Console.Write("请输入第{0}个学生的数学分数:", (i + 1));
                    st.score.shufen = double.Parse(Console.ReadLine());
                    Console.Write("请输入第{0}个学生的英语分数:", (i + 1));
                    st.score.yingfen = double.Parse(Console.ReadLine());
                    al.Add(st);
                }
                for (int i = 0; i < al.Count - 1; i++)
                {
                    for (int j = i + 1; j < al.Count; j++)
                    {
                        Student s1 = (Student)al[i];
                        Student s2 = (Student)al[j];
                        if (s1.score.yufen < s2.score.yufen)
                        {
                            Object b = al[i];
                            al[i] = al[j];
                            al[j] = b;
                        }
                    }
                }
                Student yu = (Student)al[0];
                Student yu1 = (Student)al[1];
                Console.Write("语文成绩最高的2个是:" + yu.xuehao + "	" + yu.name + "	" + yu.score.yufen + "	" + yu.score.shufen + "	" + yu.score.yingfen + "
    ");
                Console.Write(yu1.xuehao + "	" + yu1.name + "	" + yu1.score.yufen + "	" + yu1.score.shufen + "	" + yu1.score.yingfen + "
    ");
                for (int i = 0; i < al.Count - 1; i++)
                {
                    for (int j = i + 1; j < al.Count; j++)
                    {
                        Student s1 = (Student)al[i];
                        Student s2 = (Student)al[j];
                        if (s1.score.shufen < s2.score.shufen)
                        {
                            Object b = al[i];
                            al[i] = al[j];
                            al[j] = b;
                        }
                    }
                }
                Student shu = (Student)al[0];
                Student shu1 = (Student)al[1];
                Console.Write("数学成绩最高的2个是:" + shu.xuehao + "	" + shu.name + "	" + shu.score.yufen + "	" + shu.score.shufen + "	" + shu.score.yingfen + "
    ");
                Console.Write(shu1.xuehao + "	" + shu1.name + "	" + shu1.score.yufen + "	" + shu1.score.shufen + "	" + shu1.score.yingfen + "
    ");
                double sum = 0;
                for (int i = 0; i < al.Count; i++)
                {
                    Student s1 = (Student)al[i];
                    sum += s1.score.yingfen;
                }
                Console.WriteLine("英语平均分:" + sum / a);
    
                Console.ReadLine();
    
    

    车辆信息

     //所有在售车辆中最便宜的
     
                //求宝马中最便宜的车型是什么,所有信息列出
     
                //选个轴距最长的
     
                //选个轴距最长的,必须是奔驰
     
                //想要一辆油耗最低的车
     
                //想要一辆油耗最低的奥迪车
     
                //我只有50万,看看能买什么车
     
                //我只需要60万以上的车,列出来所有车型和所有信息
     
                //轴距必须超过3m,列列表
     
                //油耗在8.5以下都行,列列表
     struct che
            {
                public string xh;
                public double jg;
                public double zj;
                public double yh;
            }
                       ArrayList al = new ArrayList();
                che b1 = new che(); che b2 = new che(); che b3 = new che(); che a1 = new che(); che a2 = new che(); che a3 = new che(); che be1 = new che(); che be2 = new che(); che be3 = new che();
    
                b1.xh = "宝马320Li"; b1.jg = 38; b1.zj = 2920; b1.yh = 6.9;
                b2.xh = "宝马520Li"; b2.jg = 43; b2.zj = 3108; b2.yh = 7.2;
                b3.xh = "宝马730Li"; b3.jg = 89; b3.zj = 3210; b3.yh = 6.3;
                a1.xh = "奥迪A4L35TFSI"; a1.jg = 31; a1.zj = 2869; a1.yh = 6.2;
                a2.xh = "奥迪A6L30TFSI"; a2.jg = 43; a2.zj = 3012; a2.yh = 7.6;
                a3.xh = "奥迪A8L45TFSI"; a3.jg = 89; a3.zj = 3122; a3.yh = 8.1;
                be1.xh = "奔驰C200L"; be1.jg = 35; be1.zj = 2920; be1.yh = 6.1;
                be2.xh = "奔驰E260L"; be2.jg = 48; be2.zj = 3014; be2.yh = 6.7;
                be3.xh = "奔驰S320L"; be3.jg = 93; be3.zj = 3165; be3.yh = 8;
                al.Add(b1); al.Add(b2); al.Add(b3); al.Add(b1); al.Add(a1); al.Add(a2); al.Add(a3); al.Add(be1); al.Add(be2); al.Add(be3);
                #region
                for (int i = 0; i < 8; i++)
                {
                    for (int j = i + 1; j < 9; j++)
                    {
                        if (((che)al[i]).jg > ((che)al[j]).jg)
                        {
                            object b = al[i];
                            al[i] = al[j];
                            al[j] = b;
                        }
                    }
                }
                Console.Write("最便宜的车是:" + ((che)al[0]).xh + "	" + ((che)al[0]).jg + "	" + ((che)al[0]).zj + "	" + ((che)al[0]).yh + "
    ");
                 下面的方法只能在上面排好顺序后才可以使用!!!
                for (int i = 0; i < 9; i++)
                {
                    if (((che)al[i]).xh.Contains("宝马"))
                    {
                        che baocheap = (che)al[i];
                        Console.Write("最便宜的宝马车是:" + baocheap.xh + "	" + baocheap.jg + "	" + baocheap.zj + "	" + baocheap.yh + "
    ");
                        break;
                    }
                }
                ArrayList bao = new ArrayList();
                for (int i = 0; i < 9; i++)
                {
                    if (((che)al[i]).xh.Contains("宝马"))
                    {
                        bao.Add(al[i]);
                    }
                }
                for (int i = 0; i < bao.Count - 1; i++)
                {
                    for (int j = i + 1; j < bao.Count; j++)
                    {
                        if (((che)bao[i]).jg > ((che)bao[j]).jg)
                        {
                            object b = bao[i];
                            bao[i] = bao[j];
                            bao[j] = b;
                        }
                    }
                }
                Console.Write("宝马里最便宜的车是:" + ((che)bao[0]).xh + "	" + ((che)bao[0]).jg + "	" + ((che)bao[0]).zj + "	" + ((che)bao[0]).yh + "
    ");
                #endregion
                #region
    
                for (int i = 0; i < 8; i++)
                {
                    for (int j = i + 1; j < 9; j++)
                    {
                        if (((che)al[i]).zj < ((che)al[j]).zj)
                        {
                            object b = al[i];
                            al[i] = al[j];
                            al[j] = b;
                        }
                    }
                }
                Console.Write("轴距最长的车是:" + ((che)al[0]).xh + "	" + ((che)al[0]).jg + "	" + ((che)al[0]).zj + "	" + ((che)al[0]).yh + "
    ");
                ArrayList ben = new ArrayList();
                for (int i = 0; i < 9; i++)
                {
                    if (((che)al[i]).xh.Contains("宝马"))
                    {
                        ben.Add(al[i]);
                    }
                }
                for (int i = 0; i < ben.Count - 1; i++)
                {
                    for (int j = i + 1; j < ben.Count; j++)
                    {
                        if (((che)ben[i]).zj > ((che)ben[j]).zj)
                        {
                            object b = ben[i];
                            ben[i] = ben[j];
                            ben[j] = b;
                        }
                    }
                }
                Console.Write("奔驰里面轴距最长的车是:" + ((che)bao[0]).xh + "	" + ((che)bao[0]).jg + "	" + ((che)bao[0]).zj + "	" + ((che)bao[0]).yh);
                #endregion
                #region
    
                for (int i = 0; i < al.Count; i++)
                {
                    if (((che)al[i]).jg <= 50)
                    {
                        Console.Write(((che)al[i]).xh + "	" + ((che)al[i]).jg + "	" + ((che)al[i]).zj + "	" + ((che)al[i]).yh + "
    ");
                    }
                }
                Console.WriteLine();
                for (int i = 0; i < al.Count; i++)
                {
                    if (((che)al[i]).jg > 60)
                    {
                        Console.Write(((che)al[i]).xh + "	" + ((che)al[i]).jg + "	" + ((che)al[i]).zj + "	" + ((che)al[i]).yh + "
    ");
                    }
                }
                for (int i = 0; i < al.Count; i++)
                {
                    if (((che)al[i]).zj > 3000)
                    {
                        Console.Write(((che)al[i]).xh + "	" + ((che)al[i]).jg + "	" + ((che)al[i]).zj + "	" + ((che)al[i]).yh + "
    ");
                    }
                }
                for (int i = 0; i < al.Count; i++)
                {
                    if (((che)al[i]).yh <8.5)
                    {
                        Console.Write(((che)al[i]).xh + "	" + ((che)al[i]).jg + "	" + ((che)al[i]).zj + "	" + ((che)al[i]).yh + "
    ");
                    }
                }
                 #endregion
                for (int i = 0; i < 8; i++)
                {
                    for (int j = i + 1; j < 9; j++)
                    {
                        if (((che)al[i]).yh > ((che)al[j]).yh)
                        {
                            object b = al[i];
                            al[i] = al[j];
                            al[j] = b;
                        }
                    }
                }
                Console.Write("油耗最低的车是:" + ((che)al[0]).xh + "	" + ((che)al[0]).jg + "	" + ((che)al[0]).zj + "	" + ((che)al[0]).yh + "
    ");
                ArrayList ao = new ArrayList();
                for (int i = 0; i < 9; i++)
                {
                    if (((che)al[i]).xh.Contains("奥迪"))
                    {
                       ao.Add(al[i]);
                    }
                }
                for (int i = 0; i < ao.Count - 1; i++)
                {
                    for (int j = i + 1; j < ao.Count; j++)
                    {
                        if (((che)ao[i]).jg > ((che)ao[j]).jg)
                        {
                            object b = ao[i];
                            ao[i] = ao[j];
                            ao[j] = b;
                        }
                    }
                }
                Console.Write("奥迪里面油耗最低的车是:" + ((che)ao[0]).xh + "	" + ((che)ao[0]).jg + "	" + ((che)ao[0]).zj + "	" + ((che)ao[0]).yh + "
    ");
    
    
    
                Console.ReadLine();
     
     
     
     
     
     
     
  • 相关阅读:
    CentOs7安装JDK/Tomcat/Git/Gradle
    eclipse更改web项目的WebContent目录
    (06)SpringCloud实战之Hystrix断路器
    (06)Eclipse中引入maven模块jar包显示成without test code解决方法
    (05)SpringCloud实战之Feign负载均衡
    (04)SpringCloud实战之Ribbon负载均衡
    (03)SpringCloud实战之Eureka服务注册与发现
    (02)SpringCloud实战之Rest微服务构建案例工程模块
    (01)框架整合之SpringMVC
    (02)Maven子工程继承父工程
  • 原文地址:https://www.cnblogs.com/shadow-wolf/p/5978458.html
Copyright © 2011-2022 走看看