diff --git "a/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\350\201\224\346\203\263\346\210\252\345\233\276_20230407185503.png" "b/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\350\201\224\346\203\263\346\210\252\345\233\276_20230407185503.png" new file mode 100644 index 0000000000000000000000000000000000000000..f7141515cb962f6101f88768e7565014d93311b8 Binary files /dev/null and "b/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\350\201\224\346\203\263\346\210\252\345\233\276_20230407185503.png" differ diff --git "a/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" "b/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..2ec90ab823018b38d262c4cd8e339fc307131cb1 --- /dev/null +++ "b/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,172 @@ +```c# + +namespace _3 +{ + internal class Program + { + static void Main(string[] args) + { + /* 1.求圆的面积 + + 要求用户输入半径的值,打印出以此值为半径的圆的面积 + 求圆的面积的公式:πr²。 + 圆周率π定义成常量取3.14。 + r由用户输入并存入一个变量中,此变量用double比较合适,因为用户可能会输入小数。 + */ + Console.WriteLine("请输入圆的半径:"); + const double π = 3.14f; + Double r = Double.Parse(Console.ReadLine()); + double S = π * r * r; + Console.WriteLine("圆的面积=" + S); + + + /*2.编写一个程序,请用户输入一个四位整数, + + * 将用户输入的四位数的千位、百位、十位和个位分别显示出来,如5632, + * 则显示“用户输入的千位为5,百位为6,十位为3,个位为2” + */ + Console.WriteLine("请输入一个四位整数:"); + int num1 = int.Parse(Console.ReadLine()); + Console.WriteLine("千位数是" + num1 / 1000 + "\n" + + "百位数是" + num1 % 1000 / 100 + "\n" + + "十位数是" + num1 % 1000 % 100 / 10 + "\n" + + "个位数是" + num1 % 10); + + + /* 3.用户输入三个数,找出最大的数,打印输出。*/ + + Console.WriteLine("请输入第一个三位整数:"); + int first = int.Parse(Console.ReadLine()); + Console.WriteLine("请输入第二个三位整数:"); + int second = int.Parse(Console.ReadLine()); + Console.WriteLine("请输入第三个三位整数:"); + int third = int.Parse(Console.ReadLine()); + if (first < second) + { + if (second < third) + { + Console.WriteLine("最大的数是" + third); + } + else + { + Console.WriteLine("最大的数是" + second); + } + } + else + { + Console.WriteLine("最大的数是" + first); + } + + + /*4.接受用户输入一个数n,求这个数的阶乘;5! = 5 * 4 * 3 * 2 * 1;*/ + Console.WriteLine("请输入一个整数"); + int num2 = int.Parse(Console.ReadLine()); + for (int i = num2 - 1; i >= 1; i--) + { + num2 *= i; + } + Console.WriteLine("这个数的阶乘为" + num2); + + + /* 5.接受用户输入的一个数n,求n到1所有数的阶乘和;n! + (n - 1!) + (n - 2)! +……+1!*/ + Console.WriteLine("请输入一个整数:"); + int num3 = int.Parse(Console.ReadLine()); + int sum = 0; + for (int i = 1; i <= num3; i++) + { + int box = 1; + for (int j = 1; j <= i; j++) + { + box *= j; + } + sum += box; + } + Console.WriteLine(num3 + "到1的所有数的阶乘和为" + sum); + + + /*6.根据用户输入的菱形边长,打印菱形;如边长为3,行数为5*/ + Console.WriteLine(" 请输入菱形的边长x:"); + /*菱形的边长至少为2,且行数一定为奇数*/ + int x = int.Parse(Console.ReadLine()); + /*先打出菱形的上半部分*/ + for (int i = 1; i <= x; i++) + { + for (int j = 1; j <= x - i; j++) + { + Console.Write(" "); + } + for (int k = 1; k <= 2 * i - 1; k++) + { + Console.Write("*"); + } + Console.Write("\n"); + } + /*再打出下半部分*/ + for (int i = x - 1; i >= 1; i--) + { + for (int j = 1; j <= x - i; j++) + { + Console.Write(" "); + } + for (int k = 1; k <= 2 * i - 1; k++) + { + Console.Write("*"); + } + Console.Write("\n"); + } + + + /* 7.用循环打印九九乘法表(用二维数组保存字符串后再打印)*/ + for (int i = 1; i <= 9; i++) { + for (int j = 1; j <= i; j++) { + Console.Write(i + "×" + j + "=" + i * j + "\t"); + } + Console.Write("\n"); + } + + + /*8.输入一行字符串,分别统计出其中英文字母、数字、空格的个数。*/ + Console.Write("请输入一段字符串:"); + int letter = 0; + int num4 = 0; + int space = 0; + string str = Console.ReadLine(); + char[] ch = str.ToCharArray(); + foreach (char i in ch) { + if (i >= 'a' && i <= 'z' || i >= 'A' && i <= 'Z') + { + letter++; + } + else if (i >= '0' && i <= '9') + { + num4++; + } + else if (i == ' ') { + space++; + } + } + Console.Write("字母的个数为" + letter + "个," + "\n" + + "数字的个数为" + num4 + "个," + "\n" + + "空格的个数为" + space + "个," + "\n"); + /*9.在 Main 方法中创建一个 double 类型的数组,并在该数组中存入 5 名学生的考试成绩,计算总成绩和平均成绩。(要求使用foreach语句实现该功能)*/ + double[] score = new double[5]; + double sums = 0; + double avg = 0; + Console.WriteLine("请输入五名同学的成绩:"); + foreach (int i in score) + { + score[i] = double.Parse(Console.ReadLine()); + sums += score[i]; + } + avg = sums / 5; + Console.Write("总分为"+sums+",\n"+"平均分为"+avg); + + + /* 10.定义一个方法,实现一维数组的排序功能,从大到小排序。(不要用Array类的方法)*/ + + + } + } +} +``` + diff --git "a/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" "b/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..a13a77bea9b81907fcec0e4a0437bdd018b91586 --- /dev/null +++ "b/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,124 @@ +```c# +namespace ConsoleApp2 +{ + internal class Program + { + //1. + + static void Main(string[] args) + { + Console.WriteLine("善学如春起之苗"); + Console.WriteLine("不见其增,日有所长"); + Console.WriteLine("假学如磨刀之石"); + Console.WriteLine("不见其损,年有所亏"); + Console.WriteLine("加油吧!少年"); + Console.WriteLine("J\nA\nV\nA\n!\n"); + } + } + + //2. + + internal class Homework3 + { + static void Main(string[] args) + { + Console.WriteLine("true"); + Console.WriteLine("false"); + } + } + + //3. + + internal class Homework4 + { + static void Main(string[] args) + { + sbyte num1 = -128; + byte num2 = 127; + Console.WriteLine(num1); + Console.WriteLine(num2); + + short num3 = -32768; + short num4 = 32767; + Console.WriteLine(num3); + Console.WriteLine(num4); + + int num5 = -2147483648; + int num6 = 2147483647; + Console.WriteLine(num5); + Console.WriteLine(num6); + + long num7 = -2147483649; + long num8 = 2147483648; + Console.WriteLine(num7); + Console.WriteLine(num8); + } + } + + //4. + + internal class Homwork5 + { + static void Main(string[] ags) + { + float num9 = -3.14F; + float num10 = 3.14F; + Console.WriteLine(num9); + Console.WriteLine(num10); + double num11 = -3.4; + double num12 = 3.4; + Console.WriteLine(num11); + Console.WriteLine(num12); + } + } + + //5. + internal class Homework6 + { + static void Main(string[] avgs) + { + int a = 10; + int b = 20; + int temp = a; + a = b; + b = temp; + Console.WriteLine("a的值是"+a); + Console.WriteLine("b的值是"+b); + } + } + + internal class Homework7 + { + static void Main(string[] args) + { + int x = 100; + int y = 200; + int add = x + y; + int sub = x - y; + int mul = x * y; + int div = x / y; + Console.WriteLine("x,y的和为:"+add); + Console.WriteLine("x,y的差为:"+sub); + Console.WriteLine("x,y的积为:"+mul); + Console.WriteLine("x,y的商为:"+div); + } + } + + //6. + internal class Homework8 + { + static void Main(string[] args) + { + double x = 100.8; + double y = 20.6; + double add = x + y; + double sub = x - y; + double mul = x * y; + double div = x / y; + Console.WriteLine("x,y的和为:"+add); + Console.WriteLine("x,y的差为:"+sub); + Console.WriteLine("x,y的积为:"+mul); + Console.WriteLine("x,y的商为:"+div); + +``` + diff --git "a/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\345\244\232\346\200\201.md" "b/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\345\244\232\346\200\201.md" new file mode 100644 index 0000000000000000000000000000000000000000..99d31d9650770a688a6d5ee2be8bb1bd0d44a1b3 --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\345\244\232\346\200\201.md" @@ -0,0 +1,128 @@ +```c# +using System.Security.Cryptography.X509Certificates; + +namespace ConsoleApp1 +{ + internal class Program + { + + + + + + //1、创建Calculate基类,其中包含两个整型的protected成员,用以接收用户输入的两个整数。定义一个DisplayResult()虚方法,计算并输出结果。 + //2、定义四个类继承自Calculate类,分别重写DisplayResult()方法,实现两个整数的加、减、乘、除运算,并输出结果。 + //3、根据用户输入的操作符,实例化相应的类,完成运算并输出结果。 + //4、在主类中添加一个方法,形参为父类对象,根据传递实参的类型,调用方法,实现计算和显示结果。 + + + //二、创建一个Shape(形状)类,此类包含一个名为color的数据成员,用于存储颜色,这个类还包含一个名为GetArea()的虚方法(这个方法是用来获取形状面积的)。 + //基于这个Shape,创建两个子类:Circle(圆形类)和Square(正方形类),Circle类中包含radius(半径)的数据成员,Square类中包含sideLen(边长)的数据成员,这两个子类都去重写父类的GetArea()方法,各自去实现计算自己的面积。 + + //在主类中添加一个方法,参数类型就是Shape,方法体中,用形参去调用GetArea()方法。 + //main方法中去测试这个方法。 + + static void Main(string[] args) + { + + // 一、编写一个控制台应用程序,接受用户输入的两个整数和一个操作符,以实现对两个整数的加、减、乘、除运算,并显示出计算结果。 + Console.WriteLine("请输入第一个数字"); + + int A= Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("请输入第二个数字"); + + int B = Convert.ToInt32(Console.ReadLine()); + Console.WriteLine("请输入一个运算符"); + string C = Console.ReadLine(); + + + int result; + switch (C) + { + case"+": + result =A+B; + break; + case "-": + result =A-B; + break; + case "*": + result = A + B; + break; + case "/": + result = A + B; + break; + } + Calculate(Calculate); + + + } + //在主类中添加一个方法,形参为父类对象,根据传递实参的类型,调用方法,实现计算和显示结果。 + public static void Calculate(Calculate calculate) + { + calculate.DisplayResult(): + } + } + class Calculate + { + //其中包含两个整型的protected成员,用以接收用户输入的两个整数 + protected int A; + protected int B; + public Calculate(int A, int B) + { + this.A = A; + this.B = B; + } + //定义一个DisplayResult()虚方法,计算并输出结果 + public virtual void DisplayResult() + { + //运算由子类实现 + } + + } + class Add : Calculate + { + public Add(int A, int B) : base(A, B) + { + + } + public override void DisplayResult() + { + Console.WriteLine($"{A}+{B}={A + B}"); + } + } + class Sub : Calculate + { + public Sub(int A, int B) : base(A, B) + { + + } + public override void DisplayResult() + { + Console.WriteLine($"{A}-{B}={A - B}"); + } + } + class Mul : Calculate + { + public Mul(int A, int B) : base(A, B) + { + + } + public override void DisplayResult() + { + Console.WriteLine($"{A}*{B}={A * B}"); + } + } + class Div : Calculate + { + public Div(int A, int B) : base(A, B) + { + + } + public override void DisplayResult() + { + Console.WriteLine($"{A}/{B}={A / B}"); + } + } +} +``` + diff --git "a/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\345\244\232\346\200\2012.md" "b/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\345\244\232\346\200\2012.md" new file mode 100644 index 0000000000000000000000000000000000000000..e50b91137666c4570cd36b74900592ce8c12541f --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\345\244\232\346\200\2012.md" @@ -0,0 +1,124 @@ +```c# +namespace Shape +{ + internal class Program + { + /* 二、创建一个Shape(形状)类,此类包含一个名为color的数据成员,用于存储颜色, + * 这个类还包含一个名为GetArea()的虚方法(这个方法是用来获取形状面积的)。 + + 基于这个Shape,创建两个子类:Circle(圆形类)和Square(正方形类), + Circle类中包含radius(半径)的数据成员,Square类中包含sideLen(边长)的数据成员,这两个子类都去重写父类的GetArea()方法,各自去实现计算自己的面积。 + + 在主类中添加一个方法,参数类型就是Shape,方法体中,用形参去调用GetArea()方法 + main方法中去测试这个方法。*/ + static void Main(string[] args) + { + Console.WriteLine("请输入圆形半径:"); + double r=Convert.ToDouble(Console.ReadLine()); + + Console.WriteLine("请输入图形颜色:"); + string color=Console.ReadLine(); + + Console.WriteLine("请输入矩形边长:"); + double sideLen=double.Parse(Console.ReadLine()); + Console.WriteLine("-------------------------------------------"); + + Circle circle=new Circle(color); + circle.r = r; + circle.color = color; + circle.GetArea(); + Console.WriteLine("-------------------------------------------"); + + Square square=new Square(color); + square.sideLen= sideLen; + square.color = color; + square.GetArea(); + } + + // 在主类中添加一个方法,参数类型就是Shape,方法体中,用形参去调用GetArea()方法 + public static void Shape(Shape shape) + { + shape.GetArea(); + } + } +} + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Shape +{ + //创建一个Shape(形状)类,此类包含一个名为color的数据成员,用于存储颜色, + //包含一个名为GetArea()的虚方法(这个方法是用来获取形状面积的)。 + internal class Shape + { + public string color; + + public Shape(string color) + { + this.color = color; + + } + + public virtual void GetArea() { } + } +} +//C#考点 +//如何定义类 +//类的属性方法字段 +//构造方法,有参无参,方法重载,静态方法,虚方法,继承,集合 + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Shape +{ + internal class Square:Shape + { + //Square类中包含sideLen(边长)的数据成员 + //重写父类的GetArea()方法,实现计算自己的面积。 + public double sideLen; + + + public Square(string color):base(color) { } + public override void GetArea() + { + base.GetArea(); + Console.WriteLine($"{color}的矩形面积:{sideLen*sideLen}"); + } + + } +} + + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Shape +{ + //Circle类中包含radius(半径)的数据成员 + //重写父类的GetArea()方法,实现计算自己的面积。 + internal class Circle:Shape + { + public double r; + public Circle(string color):base(color) { } + + public override void GetArea() + { + base.GetArea(); + Console.WriteLine($"{color}的圆形面积:{r*r*Math.PI}"); + } + } +} + +``` + diff --git "a/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\347\210\266\347\261\273.md" "b/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\347\210\266\347\261\273.md" new file mode 100644 index 0000000000000000000000000000000000000000..cfa64ad567795f334b8a6db7f8bf86dae546dd8d --- /dev/null +++ "b/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\347\210\266\347\261\273.md" @@ -0,0 +1,122 @@ +1. + +```c# +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 游戏作业 +{ + internal class Teacher:person + { + //职称(Title)、工资号(Wageno) + public string Title; + public string Wageno; + public void way3() + { + Console.WriteLine("职称:{0}",Title); + Console.WriteLine("工资号;{0}",Wageno); + } + } + +} +``` + +2. + +```c# + +namespace 游戏作业 +{ + public class person + { + //编号(Id)、姓名(Name)、性别(Sex)、身份证号(Cardid)、联系方式(Tel) + public int Id { get; set; } + public string Name { get; set; } + public char Sex { get; set; } + public string Cardid { get; set; } + public int Tel { get; set; } + public void way1() + { + Console.WriteLine("编号:{0}",Id); + Console.WriteLine("姓名:{0}",Name); + Console.WriteLine("性别:{0}",Sex); + Console.WriteLine("省份证号;{0}",Cardid); + Console.WriteLine("联系方式:{0}",Tel); + } + } +} +``` + +3. + +```c# +namespace 游戏作业 +{ + class role + { + //角色名字,角色介绍,角色昵称,攻击力,防御力,速度。 + public string rolename { get; set; } + public string roleintroduce { get; set; } + public int roleid { get; set; } + public int attack { get; set; } + public int def { get; set; } + public int speed { get; set; } + public string skill1 { get; set; } + public string skill2 { get; set; } + public string skill3 { get; set; } + + + + } +} + +``` + +4. + +```c# +namespace 游戏作业 +{ + class skill:role + { + public void print() + { + Console.WriteLine("角色名称:{0}",rolename); + Console.WriteLine("角色介绍:{0}", roleintroduce); + Console.WriteLine("角色id:{0}", roleid); + Console.WriteLine("攻击力:{0}", attack); + Console.WriteLine("防御力:{0}", def); + Console.WriteLine("速度:{0}", speed); + Console.WriteLine("技能一:{0}", skill1); + Console.WriteLine("技能二:{0}", skill2); + Console.WriteLine("技能三:{0}", skill3); + + } + } +} +``` + +5. + +```c# + +namespace 游戏作业 +{ + internal class student:person + { + public string Major; + public string Grade; + public void way2() + { + + //专业(Major)、年级(Grade) + Console.WriteLine("专业:{0}",Major); + Console.WriteLine("年级:{0}",Grade); + } + } +} +``` + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\273\247\346\211\2771.md" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\273\247\346\211\2771.md" new file mode 100644 index 0000000000000000000000000000000000000000..76d368cc99a00c2904e9fd273b759edd7f437733 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\273\247\346\211\2771.md" @@ -0,0 +1,101 @@ +```c# +namespace ConsoleApp1 +{ + internal class Program + { + + // 1. 定义一个员工类,作为基类,包含姓名,性别,年龄等属性;包含一个方法代表工作的行为 + + //2. 定义一个医生类,继承员工类,重写工作的方法,描述医生的具体工作 + + //3. 定义一个程序员类,继承员工类,重写工作方法,描述程序员具体工作 + + //4. 定义一个清洁工类,继承员工类,重写工作方法,描述清洁工的具体工作 + + //5. 在主方法中为每个类实例化一个对象,用每个对象调用工作方法; + + static void Main(string[] args)//实例化 + { + Cleanr cleanr= new Cleanr("赖志生",'女',3); + cleanr.behavior(); + Doctor driver= new Doctor("钱泰铭",'女',18); + driver.behavior(); + Programmer programmer = new Programmer("木翔",'男',58); + programmer.behavior(); + } + + } +} +internal class yg//定义属性 + { + public string Name { get; set; } + public char Sex { get; set; } + public double Year { get; set; } + + public yg(string name,char sex,double year)//定义一个有参 + { + this.Name=Name; + this.Sex = Sex; + this.Year = Year; + } + public yg() //定义一个无参 + { + } + public virtual void behavior()//行为 + { + Console.WriteLine(""); + } + } +internal class Cleanr:yg + { + public Cleanr(string Name,char Sex,double Year) //有参 + { + this.Name = Name; + this.Sex = Sex; + this.Year = Year; + } + public Cleanr()//无参 + { + } + public override void behavior()//行为 + { + base.behavior(); + Console.WriteLine($"{Name}正在打扫卫生"); + } + } +internal class Doctor:yg + { + public Doctor(string Name,char Sex,double Year) + { + this.Name = Name; + this.Sex = Sex; + this.Year = Year; + } + public Doctor() //无参 + { + } + public override void behavior() + { + base.behavior(); + Console.WriteLine($"{Year}的{Name}{Sex}医生正在给病人看病"); + } + }internal class Programmer:yg + { + public Programmer(string Name, char Sex, double Year) + { + this.Name = Name; + this.Sex = Sex; + this.Year = Year; + } + public Programmer() + { + + } + public override void behavior() + { + base.behavior(); + Console.WriteLine($"{Year}的{Name}正在秃头"); + } + } +``` + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\273\247\346\211\2772.md" "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\273\247\346\211\2772.md" new file mode 100644 index 0000000000000000000000000000000000000000..d54bf17328d5e568f1f3f9375a599044916c2b51 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\273\247\346\211\2772.md" @@ -0,0 +1,170 @@ +```c# +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 虚方法 +{ + //1. 动物类:具有动物的共同属性:腿,重量等;共同行为:吃,叫,睡 + internal class Animal + { + public string Name { get; set; }//姓名 + public int Leg { get; set; }//腿 + public int Weight { get; set; }//重量 + public Animal(string name, int leg, int weight) + { + Name = name; + Leg = leg; + Weight = weight; + } + + public Animal() + { + } + + + public virtual void Eat() + { + Console.WriteLine($"{Name}有{Leg}条腿,{Weight}kg,吃吃吃"); + } + public virtual void Cry() + { + Console.WriteLine($"{Name}有{Leg}条腿,{Weight}kg,叫叫叫"); + } + public virtual void Sleep() + { + Console.WriteLine($"{Name}有{Leg}条腿,{Weight}kg,睡睡睡"); + } + + internal class Bird : Animal + { + public string Name { get; set; } + public Bird() + { + } + + public Bird(string name, int leg, int weight) : base(name, leg, weight) + { + } + + public void Fly() + { + Console.WriteLine($"{Name}是鸟类动物具有飞行能力"); + } + } + + internal class Cat:Animal + { + + + public int Beard { get; set; }//胡须 + + + public Cat() + { + } + + public Cat(string name, int leg, int weight) : base(name, leg, weight) + { + } + + public void NightVision() + { + Console.WriteLine("猫科类动物具有夜视能力"); + } + } + + internal class Tiger : Cat + { + public Tiger() + { + } + + public Tiger(string name, int leg, int weight) : base(name, leg, weight) + { + } + + public override void Eat() + { + base.Eat(); + Console.WriteLine("老虎正在吃yue"); + } + public override void Cry() + { + //base.Cry(); + Console.WriteLine("老虎正在嘶吼"); + } + } + + internal class Kitty : Cat + { + + + public Kitty() + { + } + + public Kitty(string name, int leg, int weight) : base(name, leg, weight) + { + } + + public override void Eat() + { + base.Eat(); + Console.WriteLine("猫正在吃昊子"); + } + public override void Cry() + { + //base.Cry(); + Console.WriteLine("猫在学狗叫"); + } + } + internal class Eagle : Bird//老鹰 + { + public Eagle() + { + } + + public Eagle(string name, int leg, int weight) : base(name, leg, weight) + { + } + + public override void Eat() + { + base.Eat(); + Console.WriteLine("老鹰正在吃眼镜蛇"); + } + public override void Cry() + { + //base.Cry(); + Console.WriteLine("老鹰正在嘤嘤嘤的怪叫"); + } + } + + internal class Swallow : Bird + { + public Swallow() + { + } + public Swallow(string name, int leg, int weight) : base(name, leg, weight) + { + } + + public override void Eat() + { + base.Eat(); + Console.WriteLine("小燕子在吃虫子"); + } + public override void Cry() + { + //base.Cry(); + Console.WriteLine("小燕子在呜呜呜"); + } + } + + } +} +``` + diff --git "a/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.md" "b/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..90a6b88da575f441d9b61e0d8b80d86e723bbc3e --- /dev/null +++ "b/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,317 @@ +```c# + +namespace _4 +{ + internal class Program + { + static void Main(string[] args) + { + /* 1.求圆的面积 + + 要求用户输入半径的值,打印出以此值为半径的圆的面积 + 求圆的面积的公式:πr²。 + 圆周率π定义成常量取3.14。 + r由用户输入并存入一个变量中,此变量用double比较合适,因为用户可能会输入小数。 + */ + Console.WriteLine("请输入圆的半径:"); + const double π = 3.14f; + Double r = Double.Parse(Console.ReadLine()); + double S = π * r * r; + Console.WriteLine("圆的面积=" + S); + + + /*2.编写一个程序,请用户输入一个四位整数, + + * 将用户输入的四位数的千位、百位、十位和个位分别显示出来,如5632, + * 则显示“用户输入的千位为5,百位为6,十位为3,个位为2” + */ + Console.WriteLine("请输入一个四位整数:"); + int num1 = int.Parse(Console.ReadLine()); + Console.WriteLine("千位数是" + num1 / 1000 + "\n" + + "百位数是" + num1 % 1000 / 100 + "\n" + + "十位数是" + num1 % 1000 % 100 / 10 + "\n" + + "个位数是" + num1 % 10); + + + /* 3.用户输入三个数,找出最大的数,打印输出。*/ + + Console.WriteLine("请输入第一个三位整数:"); + int first = int.Parse(Console.ReadLine()); + Console.WriteLine("请输入第二个三位整数:"); + int second = int.Parse(Console.ReadLine()); + Console.WriteLine("请输入第三个三位整数:"); + int third = int.Parse(Console.ReadLine()); + if (first < second) + { + if (second < third) + { + Console.WriteLine("最大的数是" + third); + } + else + { + Console.WriteLine("最大的数是" + second); + } + } + else + { + Console.WriteLine("最大的数是" + first); + } + + + /*4.接受用户输入一个数n,求这个数的阶乘;5! = 5 * 4 * 3 * 2 * 1;*/ + + Console.WriteLine("请输入一个整数"); + int num2 = int.Parse(Console.ReadLine()); + for (int i = num2 - 1; i >= 1; i--) + { + num2 *= i; + } + Console.WriteLine("这个数的阶乘为" + num2); + + + /* 5.接受用户输入的一个数n,求n到1所有数的阶乘和;n! + (n - 1!) + (n - 2)! +……+1!*/ + + Console.WriteLine("请输入一个整数:"); + int num3 = int.Parse(Console.ReadLine()); + int sum = 0; + for (int i = 1; i <= num3; i++) + { + int box = 1; + for (int j = 1; j <= i; j++) + { + box *= j; + } + sum += box; + } + Console.WriteLine(num3 + "到1的所有数的阶乘和为" + sum); + + + /*6.根据用户输入的菱形边长,打印菱形;如边长为3,行数为5*/ + + Console.WriteLine(" 请输入菱形的边长x:"); + /*菱形的边长至少为2,且行数一定为奇数*/ + int x = int.Parse(Console.ReadLine()); + /*先打出菱形的上半部分*/ + for (int i = 1; i <= x; i++) + { + for (int j = 1; j <= x - i; j++) + { + Console.Write(" "); + } + for (int k = 1; k <= 2 * i - 1; k++) + { + Console.Write("*"); + } + Console.Write("\n"); + } + /*再打出下半部分*/ + for (int i = x - 1; i >= 1; i--) + { + for (int j = 1; j <= x - i; j++) + { + Console.Write(" "); + } + for (int k = 1; k <= 2 * i - 1; k++) + { + Console.Write("*"); + } + Console.Write("\n"); + } + + + /* 7.用循环打印九九乘法表(用二维数组保存字符串后再打印)*/ + + for (int i = 1; i <= 9; i++) + { + for (int j = 1; j <= i; j++) + { + Console.Write(i + "×" + j + "=" + i * j + "\t"); + } + Console.Write("\n"); + } + + + /*8.输入一行字符串,分别统计出其中英文字母、数字、空格的个数。*/ + + Console.Write("请输入一段字符串:"); + int letter = 0; + int num4 = 0; + int space = 0; + string str = Console.ReadLine(); + char[] ch = str.ToCharArray(); + foreach (char i in ch) + { + if (i >= 'a' && i <= 'z' || i >= 'A' && i <= 'Z') + { + letter++; + } + else if (i >= '0' && i <= '9') + { + num4++; + } + else if (i == ' ') + { + space++; + } + } + Console.Write("字母的个数为" + letter + "个," + "\n" + + "数字的个数为" + num4 + "个," + "\n" + + "空格的个数为" + space + "个," + "\n"); + /*9.在 Main 方法中创建一个 double 类型的数组,并在该数组中存入 5 名学生的考试成绩,计算总成绩和平均成绩。(要求使用foreach语句实现该功能)*/ + + double[] score = new double[5]; + double sums = 0; + double avg = 0; + Console.WriteLine("请输入五名同学的成绩:"); + foreach (int i in score) + { + score[i] = double.Parse(Console.ReadLine()); + sums += score[i]; + } + avg = sums / 5; + Console.WriteLine("总分为" + sums + ",\n" + "平均分为" + avg); + + + /* 10.定义一个方法,实现一维数组的排序功能,从大到小排序。(不要用Array类的方法)*/ + + Console.WriteLine("请输入数组的长度:"); + int length = Convert.ToInt32(Console.ReadLine()); + double[] arr = new double[length]; + for (int i = 0; i < length; i++) + { + Console.WriteLine($"请输入第{i + 1}个数"); + arr[i] = double.Parse(Console.ReadLine()); + } + for (int i = 0; i < length; i++) + { + double max = arr[i]; + int maxIndex = i; + for (int j = i + 1; j < length; j++) + { + if (max < arr[j]) + { + max = arr[j]; + maxIndex = j; + arr[j] = arr[i]; + arr[i] = max; + } + } + Console.Write($"{arr[i]}\t"); + } + + + /*11. 用户输入正方形边长,用*打印出实心正方形。 + + 如:用户输入5,则输出如下图形。 + + + * * * * * + * * * * * + * * * * * + * * * * * + * * * * * + */ + Console.WriteLine("请输入正方形的边长l:"); + int l = Convert.ToInt32(Console.ReadLine()); + for (int i = 1; i <= l; i++) + { + for (int j = 1; j <= l; j++) + { + Console.Write("* "); + } + Console.WriteLine(); + } + + + /* 12.用户输入正方形边长,用* 打印出空心正方形。 + + 如:用户输入5,则输出如下图形。 + + * * * * * + * * + * * + * * + * * * * * + */ + Console.WriteLine("请输入正方形的边长a:"); + int a = Convert.ToInt32(Console.ReadLine()); + for (int i = 1; i <= a; i++) + { + if (i == 1 || i == a) + { + for (int j = 1; j <= a; j++) + { + Console.Write("* "); + } + } + else + { + for (int k = 1; k <= 2 * a - 1; k++) + { + if (k == 1 || k == 2 * a - 1) + { + Console.Write("*"); + } + else + { + Console.Write(" "); + } + } + } + Console.WriteLine(); + } + + + /*13.实现查找数组元素索引的功能。 + + 定义一个数组,然后控制台输入要查找的元素, + 返回输入值在数组中最后一次出现的位置。 + 若是找不到,请打印找不到。(不要用Array类的方法)*/ + + int[] arr2 = new int[20] { 1, 6, 3, 4, 8, 6, 3, 2, 7, 9, 10, 3, 1, 4, 5, 5, 2, 3, 1, 8 }; + Console.WriteLine("请输入一个数,查找他是否存在数组中:"); + double num5 = Convert.ToDouble(Console.ReadLine()); + int index = 0; + int count = 0; + for (int i = 0; i < arr2.Length; i++) + { + if (num5 == arr2[i]) + { + index = i; + count++; + } + } + if (count == 0) + { + Console.WriteLine("找不到"); + } + else + { + Console.WriteLine($"这个数最后一次出现在数组的第{index + 1}位"); + } + + + /*14.在 Main 方法中创建一个字符串类型的数组,并存入 5 个值,然后将数组中下标是偶数的元素输出。0是偶数*/ + + string[] arr3 = new string[5] { "1", "2", "3", "4", "5" }; + for (int i = 0; i <= arr3.Length; i++) + { + if (i % 2 == 0) + { + Console.Write(arr3[i] + "\t"); + } + } + + + /** + * 15.用二维数组存放数据,实现杨辉三角形的打印 + 1、每个数等于它上方两数之和。 + 2、每行 数字左右对称,由1开始逐渐变大。 + 3、第n行的数字有n项。 + 4、第n行数字和为2 n-1。 + */ + } + } +} +``` +