• C#学习笔记之——一些练习(包含了一些out的使用,string的使用,StringBuilder的使用,类的属性,最大公约数的求法,还有英雄,武器类的设置)


    题太多了,看注释好了= =

    包含了一些out的使用,string的使用,StringBuilder的使用,类的属性,最大公约数的求法,还有英雄,武器类的设置

    using System;
    using System.Text;
    
    namespace Homework12_6
    {
    	
    //			B:
    //			1、定义分数(Fraction)类:	
    //			(1)成员变量
    //				私有字段以及可读可写属性:分子、分母
    //			(2)成员方法:
    //				* 打印分数信息(例如: 1 / 3)
    //				* 约分
    //				* 创建一个方法交换分子和分母的值
    //				* 创建一个方法能同时得到加、减、乘、除、求余运算;
    
    	public class Fraction
    	{
    		int numerator;//分子
    		int denominator;//分母
    		public int Numerator{  //属性
    			set{
    				numerator = value;
    			}
    			get{
    				return numerator;
    			}
    		}
    		public int Denominator{  //属性
    			set{
    				if (value == 0)
    					throw new FormatException ("It can not be 0!");//抛出异常
    				denominator = value;
    			}
    			get{
    				return denominator;
    			}
    		}
    		#region Method/Function //对编程无影响
    		//打印分数信息
    		public void ShowFraction(){
    			Console.WriteLine (numerator + " " + "/" + " " + denominator);
    		}
    		//约分
    		public void ToReduce(){
    			int a = numerator, b = denominator;
    			//求出最大公约数
    			while ( a!=b)   
    				if (a>b)  a=a-b;
    
    				else  b=b-a;
    			numerator /= a;
    			denominator /= a;
    		}
    		//交换分子和分母的值
    		public void Swap(){
    			int temp;
    			temp = this.numerator;
    			this.numerator = this.denominator;
    			this.denominator = temp;
    		}
    		//同时得到加、减、乘、除、求余运算
    		public void Calculate (Fraction fraction) {
    			Console.WriteLine ("相加结果是:", (this.numerator/this.denominator+fraction.numerator/fraction.denominator));
    			Console.WriteLine ("相减结果是:", (this.numerator/this.denominator-fraction.numerator/fraction.denominator));
    			Console.WriteLine ("相乘结果是:", (this.numerator*fraction.numerator)/(this.denominator*fraction.denominator));
    			Console.WriteLine ("相除结果是:", (this.numerator*fraction.denominator)/(this.denominator*fraction.numerator));
    			Console.WriteLine ("求余结果是:", (this.numerator * fraction.denominator) % (this.denominator * fraction.numerator));
    		}
    		public void Calculator (Fraction fraction, out Fraction addResult, out Fraction reduceResult, out Fraction mulitResult,out Fraction divResult, out Fraction comResult) {
    			addResult = new Fraction ();
    			addResult.numerator = (this.numerator * fraction.denominator) + (fraction.numerator * this.denominator);
    			addResult.denominator = this.denominator * fraction.denominator;
    			addResult.ToReduce ();
    
    			reduceResult = new Fraction ();
    			reduceResult.numerator = (this.numerator * fraction.denominator) - (fraction.numerator * this.denominator);
    			reduceResult.denominator = this.denominator * fraction.denominator;
    			reduceResult.ToReduce ();
    
    			mulitResult = new Fraction ();
    			mulitResult.numerator = this.numerator * fraction.numerator;
    			mulitResult.denominator = this.denominator * fraction.denominator;
    			mulitResult.ToReduce ();
    
    			divResult = new Fraction ();
    			divResult.numerator = this.numerator * fraction.denominator;
    			divResult.denominator = this.denominator * fraction.numerator;
    			divResult.ToReduce ();
    
    			comResult = new Fraction ();
    			comResult.numerator = (this.numerator * fraction.denominator) % (fraction.numerator * this.denominator);
    			comResult.denominator = this.denominator * fraction.denominator;
    			comResult.ToReduce ();
    		}
    		#endregion
    	}
    
    	//			C:
    	//			1、创建武器类,包含字段:加攻击力,加生命值,加防御力,加速度值,类型(包括攻击,防御,法术,移动,打野
    	//			创建英雄类,包含字段:血量,攻击力,名字,防御力,移动速度,等级
    	//			英雄类当中包括的方法:
    	//			(1)加装装备
    	//			(2)卸载装备
    	//			(3)攻击英雄
    	//			(4)受到攻击会掉血,举例:攻击力为100的时候,掉血为100。当血量小于等于0的时候英雄阵亡
    	//			(5)每杀一个英雄等级+1,同时攻击力增加200
    	//				模拟英雄互相伤害的场景
    	public enum Type
    	{
    		attack,//攻击
    		defence,//防御
    		magic,//法术
    		movement,//移动
    		jungle//打野
    	}
    	//武器类
    	public class Weapons
    	{
    		public int addATK = 0;
    		public int addHP = 0;
    		public int addDefence = 0;
    		public int addSpeed = 0;
    		public Type type;
    		public Weapons(Type type){
    			if (type == Type.attack)
    				addATK = 100;
    			if (type == Type.defence)
    				addDefence = 100;
    			if (type == Type.jungle) {
    				addATK = 50;
    				addHP = 50;
    			}
    			if (type == Type.magic) {
    				addSpeed = 10;
    				addATK = 20;
    			}
    			if (type == Type.movement)
    				addSpeed = 50;
    		}
    	}
    	//英雄类
    	public class Hero
    	{
    		string name;
    		int HP = 100;
    		int ATK;
    		int defence;
    		int speed;
    		int level = 0;
    		//init
    		public Hero(string name,int HP,int ATK,int defence,int speed)
    		{
    			this.ATK = ATK;
    			this.defence = defence;
    			this.name = name;
    			this.speed = speed;
    			this.HP = HP;
    		}
    		public void Introduce ()
    		{
    			Console.WriteLine ("【my name is {0}】", name);
    		}
    		//加装备
    		public void LoadWeapon (Weapons weapon) {
    			switch (weapon.type) {
    			case Type.attack:
    				this.ATK += weapon.addATK;
    				this.defence += weapon.addDefence;
    				this.HP += weapon.addHP;
    				this.speed += weapon.addSpeed;
    				break;
    			case Type.defence:
    				this.ATK += weapon.addATK;
    				this.defence += weapon.addDefence;
    				this.HP += weapon.addHP;
    				this.speed += weapon.addSpeed;
    				break;
    			case Type.jungle:
    				this.ATK += weapon.addATK;
    				this.defence += weapon.addDefence;
    				this.HP += weapon.addHP;
    				this.speed += weapon.addSpeed;
    				break;
    			case Type.magic:
    				this.ATK += weapon.addATK;
    				this.defence += weapon.addDefence;
    				this.HP += weapon.addHP;
    				this.speed += weapon.addSpeed;
    				break;
    			case Type.movement:
    				this.ATK += weapon.addATK;
    				this.defence += weapon.addDefence;
    				this.HP += weapon.addHP;
    				this.speed += weapon.addSpeed;
    				break;
    			}
    		}
    		//卸装备
    		public void UnloadWeapon (Weapons weapon) {
    			switch (weapon.type) {
    			case Type.attack:
    				this.ATK -= weapon.addATK;
    				this.defence -= weapon.addDefence;
    				this.HP -= weapon.addHP;
    				this.speed -= weapon.addSpeed;
    				break;
    			case Type.defence:
    				this.ATK -= weapon.addATK;
    				this.defence -= weapon.addDefence;
    				this.HP -= weapon.addHP;
    				this.speed -= weapon.addSpeed;
    				break;
    			case Type.jungle:
    				this.ATK -= weapon.addATK;
    				this.defence -= weapon.addDefence;
    				this.HP -= weapon.addHP;
    				this.speed -= weapon.addSpeed;
    				break;
    			case Type.magic:
    				this.ATK -= weapon.addATK;
    				this.defence -= weapon.addDefence;
    				this.HP -= weapon.addHP;
    				this.speed -= weapon.addSpeed;
    				break;
    			case Type.movement:
    				this.ATK -= weapon.addATK;
    				this.defence -= weapon.addDefence;
    				this.HP -= weapon.addHP;
    				this.speed -= weapon.addSpeed;
    				break;
    			}
    		}
    		//攻击
    		public void Attack (Hero hero) {
    			if (hero.HP > 0) {
    				hero.HP -= this.ATK;
    				Console.WriteLine ("【攻击{0}】",hero.name);
    				if (hero.HP <= 0) {
    					this.level++;
    				}
    			}
    		}
    		//受到攻击会掉血,举例:攻击力为100的时候,掉血为100。当血量小于等于0的时候英雄阵亡
    		public void BeAttacked (Hero[] hero) {
    			for (int i = 0; i < hero.Length; i++) {
    				if (this.HP <= 0) {
    					Console.WriteLine ("英雄死亡");
    					break;
    				} else {
    					this.HP -= hero[i].ATK;
    					Console.WriteLine ("【{0}受到{1}点攻击】",this.name,hero[i].ATK);
    				}
    					
    			}
    
    		}
    	}
    
    	class MainClass
    	{
    		public static void Main (string[] args)
    		{
    //			A:
    //			1. 自选一行英文语句,统计其中的单词个数(规定:单词之间以空格分隔),并将每一个单词的第一个字母改为大写
    			string a1 = "there are so many people around the buiding";
    			string[] a2 = a1.Split (' ');
    			Console.WriteLine ("单词个数为:" + a2.Length);
    			string a3 = "";
    			for (int i = 0; i < a2.Length; i++) {
    				a3 = a3 + a2 [i].Substring (0, 1).ToUpper () + a2[i].Substring(1) + " ";
    			}
    			Console.Write("将首字母改为大写:"+a3);
    			Console.WriteLine ();
    			//方法二
    //			for (int i = 0; i < a1.Length; i++) {
    //				StringBuilder a4 = new StringBuilder (a1 [i]);
    //				if (a4 [0] <= 'z' && a4 [0] >= 'a') {
    //					a4[0] -= ' ';
    //				}
    //				a1 [i] = a4.ToString ();
    //				Console.WriteLine (a1 [i]);
    //			}
    
    
    //			2. 求{"a","ba","bc","bad","abcde"}这个字符串数组中,字母a出现的次数
    			string[] a4 = new string[5]{"a","ba","bc","bad","abcde"};
    //			Console.WriteLine (a4.Length);
    			int count = 0;
    			for (int i = 0; i < a4.Length; i++) {
    				if (a4 [i].Contains ("a"))
    					count++;
    			}
    			Console.WriteLine ("字母a出现的次数:"+count);
    
    
    			//初始化分数
    			Console.WriteLine("第一个分数:");
    			Fraction fraction1 = new Fraction ();
    			fraction1.Numerator = 3;
    			fraction1.Denominator = 4;
    			fraction1.ShowFraction ();
    
    			Console.WriteLine("第二个分数:");
    			Fraction fraction2 = new Fraction ();
    			fraction2.Numerator = 2;
    			fraction2.Denominator = 4;
    			fraction2.ShowFraction ();
    
    			//约分
    			fraction2.ToReduce();
    			Console.WriteLine("约分结果:");
    			fraction2.ShowFraction ();
    
    			//交换分子分母的值
    			Console.WriteLine("交换分子分母的值:");
    			fraction1.Swap ();
    			fraction1.ShowFraction ();
    
    
    			//加减乘除
    			Fraction addR;
    			Fraction redR;
    			Fraction mulR;
    			Fraction divR;
    			Fraction comR;
    			fraction1.Calculate(fraction2);
    			fraction1.Calculator (fraction2, out addR, out redR, out mulR, out divR, out comR);
    			addR.ShowFraction ();
    			redR.ShowFraction ();
    			mulR.ShowFraction ();
    			divR.ShowFraction ();
    			comR.ShowFraction ();
    
    
    			//初始化武器
    			Weapons weapon1 = new Weapons(Type.attack);
    			Weapons weapon2 = new Weapons(Type.defence);
    			Weapons weapon3 = new Weapons(Type.jungle);
    			Weapons weapon4 = new Weapons(Type.magic);
    			Weapons weapon5 = new Weapons(Type.movement);
    
    			//初始化五个英雄
    			Hero hero1 = new Hero("Ash",200,50,30,20);
    			Hero hero2 = new Hero("Blenk",200,50,30,20);
    			Hero hero3 = new Hero("Christina",200,50,30,20);
    			Hero hero4 = new Hero("Elberhan",200,50,30,20);
    			Hero hero5 = new Hero("Mick",200,50,30,20);
    
    			//加装备
    			hero1.LoadWeapon(weapon1);
    			hero2.LoadWeapon(weapon2);
    			hero3.LoadWeapon(weapon3);
    			hero4.LoadWeapon(weapon4);
    			hero5.LoadWeapon(weapon5);
    
    			Hero[] heros = new Hero[4]{hero2,hero3,hero4,hero5};
    
    			hero1.Attack (hero2);
    			hero1.Attack (hero3);
    			hero1.Attack (hero4);
    			hero1.Attack (hero5);
    
    			hero1.BeAttacked (heros);
    		}
    	}
    }
    


  • 相关阅读:
    利用快慢指针快速得到链表中间节点
    idea编译golang插件总结
    Jquery复习(二)之stop()易忘点
    Jquery复习(一)之animate()易忘点
    doT学习(三)之实战
    doT学习(二)之用法集合
    doT学习(一)之语法
    npm学习(十二)之高级用法
    npm学习(十一)之package-lock.json
    npm学习(十)之如何使用创建、发布、使用作用域包
  • 原文地址:https://www.cnblogs.com/AlinaL/p/12852193.html
Copyright © 2020-2023  润新知