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+"\n");
    }   
};

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+"\n");
    }   
};

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("\n"+name+"的攻击值最大为:"+max+"\n");
        
        // 计算每把武器的平均战力值
        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("\n装备按照攻击力升序排序:");
        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);
    }
}
posted @ 2021-11-01 07:33  MrFlySand-飞沙  阅读(117)  评论(0编辑  收藏  举报