zoukankan      html  css  js  c++  java
  • C#教程

    <style type="text/css">		
    	div#editor {
    	    height: 1000px !important;
    	}
    	.ace_scroller {
    	    height: 1000px !important;
    	}
    	.ace_content {
    	    height: 1000px !important;
    	}
    </style>
    

    结构体练习

    创建装备对象的属性值

    using System;
    using System.Text;
    
    struct Arms
    {
        // 此结构体为一件装备,包含名称,攻击⼒,稳定性,距离
        public string name;
        public int attack;
        public int stablize;
        public int distance;
    
        // 构造函数
        public void ArmSetValues(string name, int attack, int stablize, int distance){
            this.name = name;
            this.attack = attack;
            this.stablize = stablize;
            this.distance = distance;
        }
    
        // 显示属性值
        public void Display(){
            Console.WriteLine("* 名称:"+name);
            Console.WriteLine("攻击力:"+attack);
            Console.WriteLine("稳定性:"+stablize);
            Console.WriteLine("距离:"+distance+"
    ");
        }   
    };
    
    public class testStructure{    
        public static void Main(string[] args){
            // 创建结构体数组
            Arms arm1 = new Arms();
            Arms arm2 = new Arms();
            Arms arm3 = new Arms();
    
            // 设置武器的属性值
            arm1.ArmSetValues("AK47", 100, 50, 100);
            arm2.ArmSetValues("AN94", 70, 70, 95);
            arm3.ArmSetValues("M4A1", 80, 100, 100);
    
            // 显示属性值
            arm1.Display();          
            arm2.Display();          
            arm3.Display();      
        }
    }
    

    输出结构体数组中装备的名称

    using System;
    using System.Text;
    
    struct Equipment
    {
        //此结构体为一件装备,包含名称,攻击⼒加成,法术强度加成,⾎量加成
        public string equipmentName;
        public int attackUp;
        public int spellPowerUp;
        public int bloodUp;
    
        //创建构造函数
        public Equipment(string equipmentName, int attackUp, int spellPowerUp, int bloodUp)
        {
            this.equipmentName = equipmentName;
            this.attackUp = attackUp;
            this.spellPowerUp = spellPowerUp;
            this.bloodUp = bloodUp;
        }
    
    }
    
    public class testStructure{
        public static void Main(string[] args){
            //创建结构体数组
            Equipment[] testArr03 = new Equipment[5];
    
            //往数组里加入五件装备
            testArr03[0] = new Equipment("攻击之爪", 8, 0, 0);
            testArr03[1] = new Equipment("黑皇杖", 20, 0, 200);
            testArr03[2] = new Equipment("秘银锤", 16, 0, 0);
            testArr03[3] = new Equipment("圣剑", 300, 0, 0);
            testArr03[4] = new Equipment("恶魔刀锋", 24, 0, 0);
    
            Console.WriteLine(testArr03[0].equipmentName);        
        }
    }
    

    战力平均值排序

    using System;
    using System.Text;
    
    struct Arms{
        // 此结构体为一件装备,包含名称,攻击⼒,稳定性,距离
        public string name;
        public int attack;
        public int stablize;
        public int distance;
    
        // 构造函数
        public Arms(string name, int attack, int stablize, int distance){
            this.name = name;
            this.attack = attack;
            this.stablize = stablize;
            this.distance = distance;
        }
    
        // 显示属性值
        public void Display(){
            Console.WriteLine("* 名称:"+name);
            Console.WriteLine("攻击力:"+attack);
            Console.WriteLine("稳定性:"+stablize);
            Console.WriteLine("距离:"+distance+"
    ");
        }   
    };
    
    public class testStructure{        
        public static void Main(string[] args){
            // 创建结构体数组
            Arms [] arm = new Arms[5];
            
            // 往数组里加入装备
            arm[0] = new Arms("AK47", 60, 70, 100);
            arm[1] = new Arms("AN94", 55, 70, 95);
            arm[2] = new Arms("M4A1", 50, 100, 100);
            arm[3] = new Arms("巴雷特", 90, 100, 100);
            arm[4] = new Arms("天龙", 80, 100, 100);
    
            // 输出所有武器的名称
            // 因为并不知道arm的数据类型是什么,所以可以用var来声明item,item就是变化的对象
            Console.Write("所有武器的名称为:");
            foreach(var item in arm){
                Console.Write(item.name + "  ");
            }
            
            // 找出攻击力最强的武器名称和攻击值
            int max = arm[0].attack;
            string name = arm[0].name;
            foreach(var ement in arm){
                if(max < ement.attack){
                    max = ement.attack;
                    name = ement.name;
                }   
            }
            Console.WriteLine("
    "+name+"的攻击值最大为:"+max+"
    ");
            
            // 计算每把武器的平均战力值
            int averageValue;
            int maxAverage = -1;
            string maxAverageName = "";
            foreach(var item in arm){
                averageValue = (item.attack + item.stablize + item.distance)/3;
                if(maxAverage < averageValue){
                    maxAverage = averageValue;
                    maxAverageName = item.name;
                }
                Console.WriteLine(item.name + "的平均战力值是:" + averageValue);
            }
            Console.WriteLine("* " + maxAverageName + "的平均战力值最高的是:" + maxAverage);
    
            // 将装备按照攻击力升序排序
            for(int i = 0; i < arm.Length; i++){
                int minValueAttack = arm[i].attack;
                var minValue = arm[i];
                for(int j = i + 1; j < arm.Length; j++){
                    if(minValueAttack > arm[j].attack){
                        minValue = arm[j];
                        arm[j] = arm[i];
                        arm[i] = minValue;
                    }
                }
            }
            Console.WriteLine("
    装备按照攻击力升序排序:");
            foreach(var item in arm){
                Console.WriteLine("{0}:{1} {2} {3}", item.name, item.attack, item.stablize, item.distance);
            }
        }
    }
    

    结构体传参做函数

    • 函数格式void FunName(Object structName),函数调用格式FunName(structName)
    • 注意:当函数参数是Object时,函数里面的内容不能有foreach。如下代码应会报错
    using System;
    using System.Text;
    
    struct Arms{
        public string name;
        public int attack;
        public int stablize;
        public int distance;
    
        public Arms(string name, int attack, int stablize, int distance){
            this.name = name;
            this.attack = attack;
            this.stablize = stablize;
            this.distance = distance;
        }    
    };
    
    public class testStructure{        
        public static void MaxAverageName(Object arm){
            Console.WriteLine("12");
            // 计算每把武器的平均战力值Object arm
            int averageValue;
            int maxAverage = -1;
            string maxAverageName = "";
            foreach(var item in arm){
                averageValue = (item.attack + item.stablize + item.distance)/3;
                if(maxAverage < averageValue){
                    maxAverage = averageValue;
                    maxAverageName = item.name;
                }
                Console.WriteLine(item.name + "的平均战力值是:" + averageValue);
            }
        }
        public static void Main(string[] args){
            Arms [] arm = new Arms[2];
            arm[0] = new Arms("AK47", 60, 70, 100);
            arm[1] = new Arms("AN94", 55, 70, 95);
            MaxAverageName(arm);
        }
    }
    

    ❤️有则改之,无则加勉。如有错误、建议、疑问,评论或联系飞沙QQ:2602629646
    ❤️本文来自作者:MrFlySand,转载请注明原文链接:https://www.cnblogs.com/MrFlySand/p/15491900.html

  • 相关阅读:
    拦截器getmodel方法什么时候被调用(没搞懂有什么鸟用,自己搭的项目中用到了这个)
    Convention插件的使用(会涉及content目录,jsp必须放入这个下面才能映射成功基于注解的配置)
    Spring组件扫描<context:component-scan/>使用详解
    Eclipse从数据库逆向生成Hibernate带注解的实体类
    HibernateTool的安装和使用(Eclipse中)
    将一字符串首字母转大写
    二分(折半)查找算法
    按位与运算符
    注解及自定义注解
    根据前序遍历和中序遍历得出后序遍历
  • 原文地址:https://www.cnblogs.com/MrFlySand/p/15491900.html
Copyright © 2011-2022 走看看