1.枚举
using System;
namespace lesson1_枚举
{
enum E_MonsterType
{
Normal,//0
Boos,//1
}
enum E_PlayerType
{
main,
other,
}
#region 基本概念
#region 1.枚举是什么
//枚举是一个特别的存在
//它是一个被命名的整形常量的集合
//一般用它来表示 状态 类型 等等
#endregion
#region 2.申明枚举 和 申明枚举变量
//注意 申明枚举 和申明枚举变量 是两个概念
//申明枚举: 相当于是 创建一个自定义的枚举类型
//申明枚举变量:使用申明的自定义枚举类型 创建一个枚举变量
#endregion
#region 3.申明枚举语法
//enum E_自定义枚举名
//{
// 自定义枚举项名字,//枚举中包裹的 整形常量 第一个默认值是0 下面会一次累加
// 自定义枚举项名字1,
// 自定义枚举项名字2,
//}
//enum E_自定义枚举名
//{
// 自定义枚举项名字 =5,//5
// 自定义枚举项名字1,//6
// 自定义枚举项名字2,//7
//}
#endregion
#endregion
#region 在哪里申明枚举
//1.namespace语句块中(常用)
//2.class语句块中 struct语句块中
//注意:枚举不能在函数语句块中声明!!!
#endregion
class Program
{
static void Main(string[] args)
{
Console.WriteLine("枚举");
#region 枚举的使用
//申明枚举变量
//自定义枚举类型 变量名 = 默认值;(自定义的枚举类型.枚举项)
E_PlayerType playerType = E_PlayerType.main;
if (playerType == E_PlayerType.main)
{
Console.WriteLine("主玩家逻辑");
}
else if (playerType == E_PlayerType.other)
{
Console.WriteLine("其他玩家逻辑");
}
//枚举 和 switch 天生一对
E_MonsterType monsterType = E_MonsterType.Boos;
switch (monsterType)
{
case E_MonsterType.Normal:
//Console.WriteLine("普通怪物逻辑");
//break;
case E_MonsterType.Boos:
Console.WriteLine("Boss逻辑");
break;
default:
break;
}
#endregion
#region 枚举的类型转换
//1.枚举和int互转
int i = (int)playerType;
Console.WriteLine(i);
//int转枚举
playerType = 0;
//2.枚举和string互转
string str = playerType.ToString();
Console.WriteLine(str);
//把string转成枚举
//Parse后 第一个参数:你要转为的是哪个 枚举类型 第二个参数:用于转换的对应枚举项的字符串
//转换完毕后 是一个通用的类型 我们需要用括号强转成我们想要的目标枚举类型
playerType = (E_PlayerType)Enum.Parse(typeof(E_PlayerType),"other");
Console.WriteLine(playerType);
#endregion
#region 枚举的作用
//在游戏开发中,对象很多时候 会有许多的状态
//比如玩家 有一个动作状态 我们需要用一个变量或者标识 来表示当前玩家处于的是哪种状态
//综合考虑 可能会使用 int 来表示他的状态
//1 行走 2 待机 3 跑步 4 跳跃 等等
//枚举可以帮助我们 清晰的分清楚状态的含义
#endregion
}
}
}
枚举练习题
using System;
namespace lesson1_枚举练习题
{
/// <summary>
/// QQ状态枚举
/// </summary>
enum E_QQType
{
/// <summary>
/// 在线
/// </summary>
Online,
/// <summary>
/// 离开
/// </summary>
Leave,
/// <summary>
/// 忙碌
/// </summary>
Busy,
/// <summary>
/// 隐身
/// </summary>
Invisible,
}
enum E_CafeType
{
M,
B,
S,
}
enum E_Sex
{
/// <summary>
/// 男性
/// </summary>
Man,
/// <summary>
/// 女性
/// </summary>
Wonman,
}
enum E_Occupation
{
/// <summary>
/// 战士
/// </summary>
Warrior,
/// <summary>
/// 猎人
/// </summary>
Hunter,
/// <summary>
/// 法师
/// </summary>
Master,
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("枚举练习题");
#region 练习题一
//定义QQ状态的枚举,并提示用户选择一个在线状态,我们接受输入的数字,并将其转换成枚举类型
try
{
Console.WriteLine("请输入QQ的状态(0在线,1离开,2忙,3隐身)");
int type = int.Parse(Console.ReadLine());
E_QQType qqType = (E_QQType)type;
Console.WriteLine(qqType);
}
catch
{
Console.WriteLine("请输入数字");
}
#endregion
#region 练习题二
//用户去星巴克买咖啡,分为中杯(35元)大杯(40元)超大杯(43元)
//请用户选择要购买的类型,用户选择后,打印:您购买了xxx咖啡,花费xx元
//例如:您购买了中杯咖啡,花费了35元
try
{
Console.WriteLine("请输入你要购买的咖啡种类(0中杯35元 1大杯40元 2超大杯43元)");
int cafeType = int.Parse(Console.ReadLine());
E_CafeType cType = (E_CafeType)cafeType;
switch (cType)
{
case E_CafeType.M:
Console.WriteLine("您购买了中杯咖啡,花费了35元");
break;
case E_CafeType.B:
Console.WriteLine("您购买了大杯咖啡,花费了40元");
break;
case E_CafeType.S:
Console.WriteLine("您购买了超大杯咖啡,花费了43元");
break;
}
}
catch
{
Console.WriteLine("请输入正确类型");
}
#endregion
#region 练习题三
//请用户选择英雄性别与英雄职业,最后打印英雄的基本属性(攻击力,防御力,技能)
//性别
//男(攻击力+50,防御力+100)
//女(攻击力+150,防御力+20)
//职业
//战士(攻击力+20,防御力+100,技能:冲锋)
//猎人(攻击力+120,防御力+30,技能:假死)
//法师(攻击力+200,防御力+10,技能:奥术冲击)
//举例打印:你选择了“女性法师”,攻击力:350,防御力:30,职业技能:奥术冲击
try
{
Console.WriteLine("请输入你要选择的性别(0男 1女)");
E_Sex sex = (E_Sex)int.Parse(Console.ReadLine());
string sexStr = "";
int atk = 0;
int def = 0;
switch (sex)
{
case E_Sex.Man:
sexStr = "男性";
atk += 50;
def += 100;
break;
case E_Sex.Wonman:
sexStr = "女性";
atk += 150;
def += 20;
break;
default:
break;
}
Console.WriteLine("请输入你要选择的职业(0战士 1猎人 2法师)");
E_Occupation o = (E_Occupation)int.Parse(Console.ReadLine());
string skill = "";
string occupation = "";
switch (o)
{
case E_Occupation.Warrior:
atk += 20;
def += 100;
skill = "冲锋";
occupation = "战士";
break;
case E_Occupation.Hunter:
atk += 120;
def += 30;
skill = "假死";
occupation = "猎人";
break;
case E_Occupation.Master:
atk += 200;
def += 10;
skill = "奥术冲击";
occupation = "法师";
break;
default:
break;
}
Console.WriteLine("你选择了\"{0}{1}\",攻击力:{2},防御力:{3},职业技能:{4}",sexStr, occupation, atk, def, skill);
}
catch
{
Console.WriteLine("请输入数字");
}
#endregion
}
}
}
2.一维数组
using System;
namespace lesson2_一维数组
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("一维数组");
#region 基本概念
//数组是存储一组相同类型数据的集合
//数组分为 一维数组,多维数组,交错数组
//一般情况下 一维数组 就简称为 数组
#endregion
#region 数组的申明
//变量类型 [] 数组名;//只是申明了一个数组 但是并没有开房
//变量类型 可以是我们学过的 或者 没学过的所有变量类型
int[] arr1;
//变量类型 [] 数组名 = new 变量类型[数组的长度];
int[] arr2 = new int[5];//这种方式 相当于开了 5 个房间 但是房间里面的 int值 默认为0
//变量类型 [] 数组名 = new 变量类型[数组的长度]{内容1,内容2,内容3,内容4,内容5};
int[] arr3 = new int[5] { 1, 2, 3, 4, 5 };
//变量类型 [] 数组名 = new 变量类型[]{内容1,内容2,内容3,内容4,……};
int[] arr4 = new int[] { 1, 2, 3, 4 };//内容决定了数组的长度
//变量类型 [] 数组名 ={内容1,内容2,内容3,内容4,……}
int[] arr5 = { 1, 2, 3 };
#endregion
#region 数组的使用
int[] array = { 1, 2, 3, 4, 5 };
//1.数组的长度
Console.WriteLine(array.Length);
//2.获取数组中的元素
//数组中的下标和索引 他们是从0开始
//通过 索引下标去 获得数组中某一个元素的值时
//一定注意!!!
//不能越界
//数组的房间号 范围是 0到length-1
Console.WriteLine(array[0]);
Console.WriteLine(array[2]);
Console.WriteLine(array[4]);
//3.修改数组中的元素
array[0] = 99;
Console.WriteLine(array[0]);
//4.遍历数组
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
//5.增加数组的元素
//数组初始化以后 是不能够 直接添加新的元素的
int[] array2 = new int[6];
//搬家
for (int i = 0; i < array.Length; i++)
{
array2[i] = array[i];
}
array = array2;
//6.删除数组的元素
//数组初始化以后 是不能够 直接删除元素的
int[] array3 = new int[5];
for (int i = 0; i < array3.Length; i++)
{
array3[i] = array[i];
}
array = array3;
//7.查找数组中的元素
//99 2 3 4 5
//要查找 3 这个元素在哪个位置
//只有通过遍历才能确定 数组中 是否存储了一个目标元素
int a = 3;
for (int i = 0; i < array.Length; i++)
{
if (a == array[i])
{
Console.WriteLine("和a相等的元素在{0}索引位置", i);
break;
}
}
#endregion
//总结
//1.概念 同一变量类型的数据集合
//2.一定要掌握的知识:申明,遍历,增删查改
//3.所有的变量类型都可以申明为 数组
//4.它是用来批量存储游戏中的同一类型对象的 容器 比如 所有的怪物 所有玩家
}
}
}
一维数组练习题
using System;
namespace lesson2_一维数组练习题
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("一维数组练习题");
#region 第一题
//请创建一个维数组并赋值, 让其值与下标一样,长度为100
/*int[] a = new int[100];
for (int i = 0; i < a.Length; i++)
{
a[i] = i;
}
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}*/
#endregion
#region 第二题
//创建另一个数组b,让数组a中的每个元素的值乘以2存入到数组B中
/*int[] b=new int [100];
for (int i = 0; i < b.length; i++)
{
b[i] = 2 * a[i];
}
for (int i = 0; i < b.Length; i++)
{
Console.WriteLine(b[i]);
}*/
#endregion
#region 第三题
//随机(0~100) 生成1个长度为10的整数数组
/*int[] c = new int[10];
for (int i = 0; i < c.Length; i++)
{
Random rd = new Random();
int sjs = rd.Next(1, 100);
c[i] = sjs;
}
for (int i = 0; i < c.Length; i++)
{
Console.WriteLine(c[i]);
}*/
#endregion
#region 第四题
//从一个整数数组中找出最大值、最小值、总和、平均值(可以使用随机数1~100)
/*int[] d = new int[10];
for (int i = 0; i < d.Length; i++)
{
Random rd = new Random();
int sjs = rd.Next(1, 100);
d[i] = sjs;
}
for (int j = 0; j < d.Length; j++)
{
for (int i = 0; i < d.Length-1-j; i++)
{
if (d[i] > d[i+1])
{
int x = 0;
x = d[i];
d[i] = d[i+1];
d[i+1] = x;
}
}
}//冒泡排序
for (int i = 0; i < d.Length; i++)
{
Console.Write(" "+d[i]);
}
int sum = 0;
for (int i = 0; i < d.Length; i++)
{
sum += d[i];
}//总和
float pingjun = (float)sum / d.Length;//平均值
Console.WriteLine("最小值为{0} 最大值为{1} 总合为{2} 平均值为{3}", d[0], d[9], sum, pingjun);*/
#endregion
#region 第五题
//交换数组中的第一个和最后一 个、第二个和倒数第二个,依次类推,把数组进行反转并打印
/*int[] e = new int[10];
for (int i = 0; i < e.length; i++)
{
Random rd = new Random();
int sjs = rd.Next(1, 100);
e[i] = sjs;
}
int y = 0;
y = e[0];
e[0] = e[e.Length-1];
e[e.Length-1] = y;//交换第一个和倒数第一个
y = e[0];
e[0] = e[e.Length-1];
e[e.Length-1] = y;//交换第二个和倒数第二个
y = e[0];
e[0] = e[e.Length-1];
e[e.Length-1] = y;//交换第三个和倒数第三个
for (int i = 0; i < e.Length; i++)
{
Console.WriteLine(e[i]);
}//交换后显示
Console.WriteLine("**************");
for (int i = e.Length-1; i >=0; i--)
{
Console.WriteLine(e[i]);
}//数组反转后打印*/
#endregion
#region 第六题
//将一个整数数组的每一个元素进行如下的处理:
//如果元素是正数则将这个位置的元素值加1;
//如果元素是负数则将这个位置的元索值减1;
//如果元素是0,则不变
/*int[] f = new int[10];
for (int i = 0; i < f.Length - 1; i++)
{
Random rd = new Random();
int sjs = rd.Next(-100, 100);
f[i] = sjs;
}
for (int i = f.Length - 1; i >= 0; i--)
{
Console.WriteLine(f[i]);
}
Console.WriteLine("**************");
for (int i = f.Length - 1; i >= 0; i--)
{
if (f[i] > 0)
{
f[i] += 1;
}
else if (f[i] == 0)
{
continue;
}
else if (f[i] < 0)
{
f[i] -= 1;
}
}
for (int i = f.Length - 1; i >= 0; i--)
{
Console.WriteLine(f[i]);
}*/
#endregion
#region 第七题
//定义一个有10个元素的数组,使用for循环, 输入10名同学的数学成绩,将成绩依次存入数组,
//然后分别求出最高分和最低分,并且求出10名同学的数学平均成绩
/*float[] m = new float[10];
try
{
for (int i = 0; i < m.Length; i++)
{
Console.WriteLine("请输入第{0}个同学的数学成绩", i + 1);
m[i] = float.Parse(Console.ReadLine());
Console.WriteLine("**************");
}
}
catch
{
Console.WriteLine("请输入正确的成绩");
return ;
}
for (int j = 0; j < m.Length; j++)
{
for (int i = 0; i < m.Length - 1 - j; i++)
{
if (m[i] > m[i + 1])
{
float x = 0;
x = m[i];
m[i] = m[i + 1];
m[i + 1] = x;
}
}
}//冒泡排序
for (int i = 0; i < m.Length; i++)
{
Console.Write(" " + m[i]);
}
float sum = 0;
for (int i = 0; i < m.Length - 1; i++)
{
sum += m[i];
}
float pingjun = sum / m.Length;
Console.WriteLine("这10位同学最高分为{0} 最低分为{1} 平均数学成绩为{2}", m[m.Length - 1], m[0], pingjun);*/
#endregion
#region 第八题
//
#endregion
}
}
}
3.二维数组
using System;
namespace lesson3_二维数组
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("二维数组");
#region 基本概念
//二位数组 是使用两个下标(索引)来确定元素的数组
//两个下标可以理解成 行标 和列标
//比如矩阵
//1 2 3
//4 5 6
//可以用二维数组 int[2,3] 表示
//好比 两行 三列的数据集合
#endregion
#region 二维数组的申明
//变量类型[ ,]二维数组变量名;
int[,] arr;
//变量类型[,]二维数组变量名= new 变量类型[行,列];
int[,] arr2 = new int[3,3];
//变量类型[,]二维数组变量名= new 变量类型[行,列]{{0行内容1,0行内容2,0行内容3……},{1行内容1,1行内容2,1行内容3……}};
int[,]arr3=new int[2, 2] { { 1, 2 }, { 1, 2 } };
//变量类型[,]二维数组变量名= new 变量类型[,]{{0行内容1,0行内容2,0行内容3……},{1行内容1,1行内容2,1行内容3……}};
int[,] arr4 = new int[, ] { { 1, 2 }, { 1, 2 } };
//变量类型[,]二维数组变量名={{0行内容1,0行内容2,0行内容3……},{1行内容1,1行内容2,1行内容3……}};
int[,] arr5 ={ { 1, 2 }, { 1, 2 } };
#endregion
#region 二维数组的使用
//1.二维数组的长度
int[,] array = new int[,] { { 1, 2 ,3}, { 4, 5,6 } };
//我们要获取 行和列分别是多长
//得到多少行
Console.WriteLine(array.GetLength(0));
//得到多少列
Console.WriteLine(array.GetLength(1));
//2.获取二维数组中的元素
//注意:第一个元素的索引是0 最后一个元素的索引 肯定是长度-1
Console.WriteLine(array[0, 1]);
//3.修改二维数组中的元素
array[0, 0] = 99;
Console.WriteLine(array[0, 0]);
//4.遍历二维数组
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
//i 行
//j 列
Console.WriteLine(array[i, j]);
}
}
//5.增加数组的元素
//数组声明初始化之后 就不能在原有的基础上进行 添加或删除了
int[,] array2 = new int[3, 3];
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array2[i, j] = array[i, j];
}
}
array = array2;
array[2, 0] = 7;
array[2, 1] = 8;
array[2, 2] = 9;
//6.删除数组中的元素
int[,] array3 = new int[2, 2];
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
array2[i, j] = array[i, j];
}
}
array2 = array3;
//7.查找数组中的元素
int a = 3;
for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < array.GetLength(1); j++)
{
if (a == array[i,j])
{
Console.WriteLine("和a相等的元素在{0}{1}索引位置", i,j);
break;
}
}
}
#endregion
//总结
//1.概念 同一变量类型的 行列数据集合
//2.一定要掌握的知识:申明,遍历,增删查改
//3.所有的变量类型都可以申明为 二位数组
//4.游戏中一般用来存储 矩阵,在控制台小游戏中可以用二维数组来 表示地图格子
}
}
}
二维数组练习题
using System;
namespace lesson3_二维数组练习题
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("二维数组练习题");
#region 练习题1
//将1到10000赋值给一个二 维数组(100行100列)
/*int[,] a = new int[100, 100];
int x = 1;
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
a[i, j] = x++;
}
}
Console.WriteLine(a[5,0]);*/
#endregion
#region 练习题2
//将二维数组(4行4列)的右上半部分置零(元索随机1~100)
/* int[,] b = new int[4, 4];
for (int i = 0; i < b.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
Random rd = new Random();
int sjs = rd.Next(1, 100);
b[i,j] = sjs;
Console.Write(" " + b[i, j]);
}
Console.WriteLine();
}
for (int i = 0; i < b.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
if (i < 2 && j > 1)
{
b[i, j] = 0;
}
Console.Write(" "+b[i,j]);
}
Console.WriteLine();
}*/
#endregion
#region 练习题3
//求二维数组(3行3列)的对角线元素的和(元素随机1~10)
/*int[,] c = new int[3, 3];
int sum = 0;
for (int i = 0; i < c.GetLength(0); i++)
{
for (int j = 0; j < c.GetLength(1); j++)
{
Random rd = new Random();
int sjs = rd.Next(1, 10);
c[i, j] = sjs;
Console.Write(" " + c[i, j]);
}
Console.WriteLine();
}
for (int i = 0; i < 3; i++)
{
sum += c[i, i];
}
Console.WriteLine(sum);*/
#endregion
#region 练习题4
//求二维数组(5行5列)中最大元素值及其行列号(元素随机1~500)
/*int[,] d = new int[5, 5];
int sum = 0;
for (int i = 0; i < d.GetLength(0); i++)
{
for (int j = 0; j < d.GetLength(1); j++)
{
Random rd = new Random();
int sjs = rd.Next(1, 500);
d[i, j] = sjs;
Console.Write(" " + d[i, j]);
}
Console.WriteLine();
}
for (int i = 0; i < d.GetLength(0); i++)
{
for (int j = 0; j < d.GetLength(1); j++)
{
if (sum < d[i, j])
{
sum = d[i, j];
}
}
}
Console.WriteLine("最大的元素值为{0}", sum);
for (int i = 0; i < d.GetLength(0); i++)
{
for (int j = 0; j < d.GetLength(1); j++)
{
if (sum== d[i, j])
{
Console.WriteLine("和a相等的元素在{0} {1}索引位置", i+1, j+1);
break;
}
}
}*/
#endregion
#region 练习题5
//给一个M*N的二维数组,数组元素的值为0或者1,要求转换数组,将含有1的行和列全部置1
Console.WriteLine("请输入M和N的值");
int m = int.Parse(Console.ReadLine());
int n = int.Parse(Console.ReadLine());
int[,] e = new int[m,n];
int[,] sum = new int[m, n];
for (int i = 0; i < e.GetLength(0); i++)
{
for (int j = 0; j < e.GetLength(1); j++)
{
Random rd = new Random();
int sjs = rd.Next(0, 2);
e[i, j] = sjs;
Console.Write(" " + e[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("***********");
for (int i = 0; i < e.GetLength(0); i++)
{
for (int j = 0; j < e.GetLength(1); j++)
{
if (e[i, j] == 1)
{
for (int x=0 ; x < e.GetLength(0); x++)
{
sum[x, j] = 1;
}
for (int y = 0; y < e.GetLength(1); y++)
{
sum[i, y] = 1;
}
}
}
}
for (int i = 0; i < e.GetLength(0); i++)
{
for (int j = 0; j < e.GetLength(1); j++)
{
Console.Write(" " + sum[i, j]);
}
Console.WriteLine();
}
#endregion
}
}
}
4.交错数组
using System;
namespace lesson4_交错数组
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("非重点知识——交错数组");
#region 基本概念
//交错数组 是 数组的数组,每个维度的数量可以不同
//注意:二维数组的每行的列数相同,交错数组每行的列数可能不同
#endregion
#region 数组的申明
//变量类型[][]交错数组名;
int[][] arr1;
//变量类型[][]交错数组名= new 变量类型[行数][];
int[][] arr2 = new int[3][];
//变量类型[][] 交错数组名 = new 变量类型[行数][]{一维数组1,一维数组2,一维数组3……};
int[][] arr3 = new int[3][] { new int[] { 1,2,3},
new int[] {1,2 },
new int[] { 1}};
//变量类型[][] 交错数组名 = new 变量类型[][]{一维数组1,一维数组2,一维数组3……};
int[][] arr4 = new int[][] { new int[] { 1,2,3},
new int[] {1,2 },
new int[] { 1}};
//变量类型[][] 交错数组名 ={一维数组1,一维数组2,一维数组3……};
int[][] arr5 ={ new int[] { 1,2,3},
new int[] {1,2 },
new int[] { 1}};
#endregion
#region 数组的使用
int[][] array ={ new int[] {1,2,3},
new int[] {1,2 },
new int[] {1}};
//1.数组的长度
//行
Console.WriteLine(array.GetLength(0));
//得到某一行的列数
Console.WriteLine(array[0].Length);
//2.获取交叉数组中的元素
Console.WriteLine(array[0][1]);
//3.修改交错数组中的元素
array[0][1] = 99;
//4.遍历交错数组
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array[i].Length; j++)
{
Console.Write(array[i][j]+" ");
}
Console.WriteLine();
}
//5.增加交错数组的元素
//6.删除交错数组的元素
//7.查找交错数组中的元素
//总结
//1.概念:交错数组 可以存储同一类型的m行 不确定列的数据
//2.一定要掌握的知识:申明,遍历,增删查改
//3.所有变量类型都可以申明为 交错数组
//4.一般交错数组很少使用 了解即可
#endregion
}
}
}
5.值类型和引用类型
using System;
namespace lesson5_值类型和引用类型
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("值类型和引用类型");
#region 变量类型的复习
/* //无符号整形
byte b = 1;
ushort us = 1;
uint ui = 1;
ulong ul = 1;
//有符号整形
sbyte sb = 1;
short s = 1;
int i = 1;
long l = 1;
//浮点数
float f = 1f;
double d = 1.1;
decimal de = 1.1m;
//特殊类型
bool bo = true;
char c = 'A';
string str = "strs";*/
//复杂数据类型
//enum 枚举
//数组(一维数组,二维数组,交错数组)
//把以上学过的 变量类型 分成 值类型和引用类型
//引用类型:string, 数组, 类
//值类型:其它, 结构体
#endregion
#region 值类型和引用类型的区别
//1.使用上的区别
//值类型
int a = 10;
//引用类型
int[] arr = new int[] { 1, 2, 3, 4 };
//申明了一个b让其等于之前的a
int b = a;
//申明了一个arr2让其等于之前的arr
int[] arr2 = arr;
Console.WriteLine("a={0},b={1}", arr[0], arr2[0]);
Console.WriteLine("arr[0]={0},arr2[0]={1}", arr[0], arr2[1]);
b = 20;
arr2[0] = 5;
Console.WriteLine("修改了b和arr2[0]之后");
Console.WriteLine("a={0},b={1}", a, b);
Console.WriteLine("arr[0]={0},arr2[0]={1}", arr[0], arr2[0]);
//值类型 在相互赋值时 把内容拷贝给了对方 它变我不变
//引用类型的 相互赋值 是 让两者指向同一个值 它变我也变
//2.为什么有以上区别
//值类型 和引用类型 存储在的 内存区域 是不同的 存储方式是不同的
//所以就造成了 他们在使用上的区别
//值类型存储在 栈空间——系统分配,自动回收,小而快
//引用类型 存储在 堆空间——手动申请和释放,大而慢
arr2 = new int[] { 99, 3, 2, 1 };
Console.WriteLine("arr[0]={0},arr2[0]={1}", arr[0], arr2[0]);
#endregion
string str = "123";
string str2 = str;
str2 = "321";
Console.WriteLine(str);//123
}
}
}
6.特殊的引用类型string
using System;
namespace lesson6_特殊的引用类型string
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("特殊的引用类型string");
#region 复习值和引用类型
//值类型——它变我不变——存储在栈内存中
//无符号整形 有符号整形 浮点数 char bool 结构体
//引用类型——它变我也变——存储在堆内存中
//数组,string, 类
#endregion
#region string的它变我不变
/*string str1 = "123";
string str2 = str1;
//因为string是引用类型 按理说 应该是它变我也变
//string非常的特殊 它具备 值类型的特征 它变我不变
str2 = "321";
Console.WriteLine(str1);
Console.WriteLine(str2);*/
//string 虽然方便 但是有一个小缺点 就是频繁的 改变string 重新赋值
//会产生 内存垃圾
//优化替代方案 在c#核心中进行讲解
#endregion
//通过断点调试 在监视窗口中查看 内存信息
string str1 = "123";
string str2 = str1;
//因为string是引用类型 按理说 应该是它变我也变
//string非常的特殊 它具备 值类型的特征 它变我不变
str2 = "321";
Console.WriteLine(str1);
Console.WriteLine(str2);
int[] a = new int[] { 10 };
int[] b = a;
b = new int[5];
Console.WriteLine(a[0]);//10
}
}
}
7.函数
using System;
namespace lesson7_函数
{
class Program
{
#region 基本概念
//函数(方法)
//本质是一块具有名称的代码块
//可以使用函数(方法)的名称来执行该代码块
//函数(方法)是封装代码进行重复使用的一种机制
//函数(方法)的主要作用
//1.封装代码
//2.提升代码复用率(少写点代码)
//3.抽象行为
#endregion
#region 函数写在哪里
//1.class语句块中
//2.struct语句块中
#endregion
#region 基本语法
//
//static 返回类型 函数名(参数类型 参数名1,参数类型 参数名2……)
// {
// 函数的代码逻辑
// ……
// return;(如果有返回类型才返回)
// }
//1.关于static 不是必须的 在没有学习类和结构体之前 都是必须写的
//2-1,关于返回类型 引出一个新的关键字 void(表示没有返回值)
//2-2,返回类型 可以写任意的变量类型 14种变量类型+复杂数据类型(数组,枚举,结构体,类class)
//3.关于函数名 使用帕斯卡命名法 myName(驼峰命名法) MyName(帕斯卡命名法)
//4-1,参数不是必须的 可以有0到n个参数 参数的类型也是可以是任意类型的 14种变量类型+复杂数据类型(数组,枚举,结构体,类class)
// 多个参数的时候 需要用 逗号隔开
//4-2,参数名 驼峰命名法
//5.当返回值类型不为void时 必须通过新的关键词 return返回对应类型的内容(注意:即使是void也可以选择性使用return)
#endregion
#region 实际运用
//1.无参无返回值函数
static void SayHellow()
{
Console.WriteLine("你好,世界!");
//没有返回值 也就是返回值类型是void 可以省略
//return;
}
//2.有参无返回值函数
static void SayYourName(string name)
{
Console.WriteLine("你的名字是:{0}", name);
}
//3.无参有返回值函数
static string WhatYourName()
{
//对应返回值类型的 内容 返回出去
return "于双";
}
//4.有参有返回值函数
static int Sum(int a, int b)
{
/*int c = a + b;
return c;*/
//return后面可以是一个表达式 只要这个表达式的结果和返回值类型是一致的就行
return a + b;
}
//5.有参有多返回值函数
//传入两个数 然后计算两个数的和 以及他们两的平均数 得出结果返回出来
static int [] Calc(int a, int b)
{
int sum = a + b;
int avg = sum / 2;
/*int[] arr = { sum, avg };
return arr;*/
return new int[]{ sum,avg};
}
#endregion
#region 关于return
//即使函数没有返回值,我们也可以使用return
//return可以直接不执行之后的代码,直接防护到函数外部
static void Speak(string str)
{
if (str == "混蛋")
{
return;
}
Console.WriteLine(str);
}
#endregion
static void Main(string[] args)
{
Console.WriteLine("函数");
SayHellow();
//参数可以是 常量 变量 函数都可以
//参数 一定是传一个 能得到对应类型的表达式
string str = "于双";
//传入一个string变量
SayYourName(str);
//传入一个string常量
SayYourName("于双2");
//传入一个返回值是string的函数
SayYourName(WhatYourName());
//有返回值的函数 要不直接拿返回值来用
//要不就是拿变量 接收它的结果
string str2 = WhatYourName();
//也可以直接调用 但是返回值 对我们来说就没用了
WhatYourName();
Console.WriteLine(Sum(2, 5));
int[] arr = Calc(5,7);
Console.WriteLine(arr[0] + " "+ arr[1]);
Speak("混蛋123");
}
}
}
函数练习题
using System;
namespace lesson7_函数练习题
{
class Program
{
#region 练习题1
//写一个函数,比较两个数字的大小,返回最大值
static int A(int a, int b)
{
if (a < b)
{
a = b;
}
return a;
}
#endregion
#region 练习题2
//写一个函数,用于计算一个圆的面积和周长,并返回打印
static float[] B(float r)
{
float PI = 3.1415926f;
float a = PI * r * r;
float b = PI * 2 * r;
return new float[] { a, b };
}
#endregion
#region 练习题3
//写一个函数,求一个数组的总和,最大值,最小值,平均数
static float[] C(params float[] arr)
{
float sum = 0, max = 0, min = 0, avg = 0;
for (int i = 0; i < arr.Length; i++)
{
sum += arr[i];
}
avg = sum / arr.Length;
for (int j = 0; j < arr.Length; j++)
{
for (int i = 0; j < arr.Length - 1 - i; i++)
{
if (arr[i] > arr[i + 1])
{
float x = 0;
x = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = x;
}
}
}
max = arr[arr.Length - 1];
min = arr[0];
return new float[] { sum, max, min, avg };
}
#endregion
#region 练习题4
//写一个函数,判断你传入的参数是不是质数
static void D(int a)
{
for (int i = 2; i < a; i++)
{
bool isFind;
if (a % i != 0)
{
isFind = true;
}
else
{
isFind = false;
}
if (isFind)
{
Console.WriteLine("{0}是质数", a);
break;
}
else
{
Console.WriteLine("{0}不是质数", a);
break;
}
}
}
/* static bool F(int a)
{
bool isFind = true;
for (int i = 2; i < a; i++)
{
if (a % i == 0)
{
isFind = false;
}
}
return isFind;
}*///第二种写法
/*bool isFind = F(13);
Console.WriteLine(isFind);*///第二种写法放主函数的
#endregion
#region 练习题5
//写一个函数,判断你输入的年份是否是闰年
//闰年怕按段条件:
//年份能被400整除(2000)
//年份能被4整除,但是不能被100整除(2008)
static void E(int a)
{
bool isFind;
if (a % 400 == 0 ||( a%4==0 && a%100!=0))
{
isFind = true;
}
else
{
isFind = false;
}
if (isFind)
{
Console.WriteLine("{0}是闰年", a);
}
else
{
Console.WriteLine("{0}不是闰年", a);
}
}
#endregion
static void Main(string[] args)
{
Console.WriteLine("函数练习题");
Console.WriteLine("最大值为"+A(5, 8));
float []arr= B(3);
Console.WriteLine("圆的面积为{0},周长为{1}", arr[0], arr[1]);
float[] arr2 = C(1,5,8,9,10);
Console.WriteLine("总和为{0},最大值为{1},最小值为{2},平均值为{3}",arr2[0],arr2[1],arr2[2],arr2[3]);
D(3);
E(2008);
}
}
}
8.ref和out
using System;
namespace lesson8_ref和out
{
class Program
{
#region 学习ref和out的原因
//学习ref和out的原因
//它们可以解决 在函数内部改变外部传入的内容 里面变了 外面也要变
static void ChangeValue(int value)
{
value = 3;
}
static void ChangeArrayValue(int[] arr)
{
arr[0] = 99;
}
static void ChnageArray(int[] arr)
{
//重新申明一个数组
arr = new int[] { 10, 20, 30, 40 };
}
#endregion
#region ref和out的使用
//ref
static void ChangeValueRef(ref int value)
{
value = 3;
}
static void ChangeArrayRef(ref int []arr)
{
arr = new int[] { 100, 200, 300 };
}
//out
static void ChangeValueOut(out int value)
{
value = 4;
}
static void ChangeArrayOut(out int[] arr)
{
arr = new int[] { 999, 200, 300 };
}
#endregion
#region ref和out的区别
//1.ref传入的变量必须初始化,out不用
//2.out传入的变量必须在内部赋值,ref不用
#endregion
static void Main(string[] args)
{
Console.WriteLine("ref和out");
int a = 1;
ChangeValue(a);
Console.WriteLine(a);//1
ChangeValueRef(ref a);
Console.WriteLine(a);//3
ChangeValueOut(out a);
Console.WriteLine(a);//4
Console.WriteLine("**********");
int[] arr2 = { 1, 2, 3, 4 };
ChangeArrayValue(arr2);
Console.WriteLine(arr2[0]);//99
ChnageArray(arr2);
Console.WriteLine(arr2[0]);//99
ChangeArrayRef(ref arr2);
Console.WriteLine(arr2[0]);//100
ChangeArrayOut(out arr2);
Console.WriteLine(arr2[0]);//999
Console.WriteLine("**********");
}
}
}
ref和out练习题
using System;
namespace lesson8_ref和out练习题
{
class Program
{
static void Login()
{
string ID = "976885877";
string MI = "5201314hal";
Console.WriteLine("请输入账号和密码");
string id = Console.ReadLine();
string mi = Console.ReadLine();
if (ID != id)
{
Console.WriteLine("账号输入错误");
}
if (MI != mi)
{
Console.WriteLine("密码输入错误");
}
if (ID == id && MI == mi)
{
Console.WriteLine("账号密码输入正确");
}
}
static void Main(string[] args)
{
Console.WriteLine("ref和out练习题");
Login();
}
}
}
9.变长参数和参数默认值
using System;
namespace lesson9_变长参数和参数默认值
{
class Program
{
#region 函数语法复习
//1.静态关键词 可选 目前对于我们来说必须写
//2.返回值 没有返回值void 可以填写任意类型的变量
//3.函数名 帕斯卡命名法
//4.参数可以是0到n个 前面可以加 ref和out 用来传递想要在函数内部改变内容的变量
//5.如果返回值不是void 那么必须有return对应类型的内容 return可以打断函数语句块中的逻辑 直接停止后面的逻辑
#endregion
#region 变长参数关键词
//举例 函数要计算 n个整数的和
//变长参数关键字 params
static int sum(params int[] arr)
{
int sum = 0;
for (int i = 0; i < arr.Length; i++)
{
sum += arr[i];
}
return sum;
}
// params int[] 意味着可以传入n个int参数 n可以等于0 传入的参数会存在arr数组中
//注意:
//1.params关键字 后面必为 数组
//2.数组的类型可以是 任意类型
//3.函数的参数可以有 别的参数 和 params关键字修饰的参数
//4.函数参数中只能最多出现一个 params关键字 并且一定是在最后一组参数 前面可以有n个其他参数
#endregion
#region 参数默认值
//有参数默认值的参数 一般称为可选参数
//作用是 当调用函数时可以不传入参数 不传就会使用默认值作为参数的值
static void Speak(string str = "你好!")
{
Console.WriteLine(str);
}
//注意:
//1.支持多参数默认值 每个参数都可以有默认值
//2.如果要混用 可选参数 必须写在 普通参数后面
#endregion
//总结
//1.变长参数关键字 params
//作用:可以传入n个同类型参数 n可以是0
//注意:
//1.params 后面必须是数组 意味着只能是同一类型的可变参数
//2.变长参数只能有一个
//3.必须在所有参数最后写变长参数
//2.参数默认值(可选参数)
//作用:可以个参数默认值 使用时可以不传参 不传用默认的 传了用传的
//注意:
//1.可选参数可以有多个
//2.正常参数写在可选参数前面 可选参数只能写在所有参数的后面
static void Main(string[] args)
{
Console.WriteLine("变长参数和参数默认值");
sum(1,2,3,4,5,6);
Speak();
}
}
}
变长参数和参数默认值练习题
using System;
namespace lesson9_变长参数和参数默认值练习题
{
class Program
{
#region 练习题1
//使用params参数 求多个数字的和及平均数
static float[] A(params float[] arr)
{
float sum = 0;
float avg = 0;
for (int i = 0; i < arr.Length; i++)
{
sum += arr[i];
}
avg = sum / arr.Length;
return new float []{ sum,avg};
}
#endregion
#region 练习题2
//使用params参数 求多个数字的偶数和奇数和
static int[] B(params int[] arr)
{
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] % 2 ==0)
{
sum1+= arr[i];
}
if (arr[i] % 2 == 1)
{
sum2 += arr[i];
}
}
return new int[] { sum1, sum2 };
}
#endregion
static void Main(string[] args)
{
Console.WriteLine("变长参数和参数默认值练习题");
float[] arr2 = A(1, 4, 7, 8, 10);
Console.WriteLine("数字和为{0} 平均数为{1}", arr2[0], arr2[1]);
int[] arr3 = B(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Console.WriteLine("偶数和为{0} 奇数和为{1}", arr3[0], arr3[1]);
}
}
}
10.函数重载
using System;
namespace lesson10_函数重载
{
class Program
{
#region 函数重载
//重载概念
//在同一语句块(class或者struct)中
//函数(方法)名相同
//参数的数量不同
//或者
//参数的数量相同 但参数的类型或顺序不同
//作用
//1.命名一组功能相似的函数,减少函数名的数量,避免命名空间的污染
//2.提升程序可读性
#endregion
#region 实例
//注意:
//1.重载和返回值类型无关,只和参数类型,个数,顺序有关
//2.调用时 程序会自己更急传入的参数类型判断使用哪一个重载
static int CalcSum(int a, int b)
{
return a + b;
}
//参数数量不同
static int CalcSum(int a, int b,int c)
{
return a + b+c;
}
//数量相同 类型不同
static float CalcSum(float a, float b)
{
return a + b;
}
//数量相同 类型不同
static float CalcSum(int a, float b)
{
return a + b;
}
//数量相同 顺序不同
static float CalcSum(float b, int a)
{
return a + b;
}
//ref和out
static float CalcSum(ref float b, int a)
{
return a + b;
}
//ref 和 out也是一种变量类型,可以重载,但是不能同时使用
#endregion
//总结
//概念:同一个语句块中,函数名相同,参数数量,类型,顺序不同的函数 就称为我们的重载函数
//注意:和返回值无关
//作用:一般用来处理不同参数的同一类型的逻辑处理
//
static void Main(string[] args)
{
Console.WriteLine("函数重载");
CalcSum(1,2);
CalcSum(1.1f, 2);
CalcSum(1, 2,3);
CalcSum(1, 1.2f);
}
}
}
重载函数练习题
using System;
namespace lesson10_重载函数练习题
{
class Program
{
#region 练习题1
//请重载一个函数
//让其可以比较两个int或两个float或两个double的大小并返回较大的那个值
static int A(int a, int b)
{
if (a < b)
{
return b;
}
return a;
}
static float A(float a, float b)
{
if (a < b)
{
return b;
}
return a;
}
static double A(double a, double b)
{
if (a < b)
{
return b;
}
return a;
}
#endregion
#region 练习题2
//请重载一个函数
//让其可以比较两个int或两个float或两个double的大小并返回较大的那个值
//(用params可变参数来完成)
static int B(params int[]arr)
{
if (arr[0] < arr[1])
{
arr[0] = arr[1];
}
return arr[0];
}
static float B(params float[]arr)
{
if (arr[0] < arr[1])
{
arr[0] = arr[1];
}
return arr[0];
}
static double B(params double[]arr)
{
if (arr[0] < arr[1])
{
arr[0] = arr[1];
}
return arr[0];
}
#endregion
static void Main(string[] args)
{
Console.WriteLine("重载函数练习题");
Console.WriteLine(A(10,21.2f));
Console.WriteLine(B(10,22.2));
}
}
}
11.递归函数
using System;
namespace lesson11_递归函数
{
class Program
{
#region 基本概念
//递归函数 就是 让函数自己调用自己
//一个正确的递归函数
//1.必须有结束调用的条件
//2.用于条件判断的 这个条件 必须改变 能够达到停止的目的
#endregion
#region 实例
//用递归函数打印出 0到10
static void Fun(int a)
{
if (a > 10)
{
return;
}
Console.WriteLine(a);
a++;
Fun(a);
}
#endregion
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Fun(0);
}
}
}
递归函数练习题
using System;
namespace lesson11_递归函数练习题
{
class Program
{
#region 练习题1
//使用递归的方式打印0到10
static void A(int a)
{
if (a > 10)
{
return;
}
Console.Write(a+" ");
a++;
A(a);
}
#endregion
#region 练习题2
//传入一个值,递归求该值的阶乘 并返回 5!=1*2*3*4*5
static int B(int a)
{
if (a == 0)
{
return 1;
}
return a * B(a - 1);
}
#endregion
#region 练习题3
//使用递归求1!+2!+3!+4!……+10!
static int C(int a)
{
if (a == 1)
{
return 1;
}
return a * C(a-1);
}
static int C2(int b)
{
if (b == 1)
{
return 1;
}
return C(b) + C2(b - 1);
}
/*static int C2(int b)
{
int sum = 0;
for (int i = 1; i <= b; i++)
{
sum += C(i);
}
return sum;
}*/
#endregion
#region 练习题4
//一根竹竿长100m 每天砍掉一半,求第十天它的长度是多少(从第0天开始)
static float D(int a, float l)
{
if (a == 0)
{
return l;
}
return D(--a, l / 2.0f);
}
#endregion
#region 练习题5
//不允许使用循环语句 条件语句,在控制台中打印出1到200这200个数(提示:递归+短路)
static bool E(int a)
{
Console.Write(" " + a);
return a < 200 && E(++a);
}
#endregion
static void Main(string[] args)
{
Console.WriteLine("递归函数练习题");
A(0);
Console.WriteLine();
Console.WriteLine(B(3));
Console.WriteLine(C2(3));
Console.WriteLine(D(5, 100));
E(1);
}
}
}
12.结构体
using System;
namespace lesson12_结构体
{
#region 基本概念
//结构体 是一种自定义变量类型 类似枚举 需要自己定义
//它是数据和函数的集合
//在结构体中 可以申明各种变量和方法
#endregion
#region 基本语法
//1.结构体一般写在 namespace 语句块中
//2.结构体关键字 struct
/*struct 自定义结构体名
{
//第一部分
//变量
//第二部分
//构造函数(可选)
//第三部分
//函数
}*/
//注意 结构体名字 我们的规范 是帕斯卡命名法
#endregion
#region 实例
//表现 学生数据 的 结构体
struct Student
{
#region 访问修饰符
//修饰结构体中变量和方法 是否能被外部使用
//public 公共的 可以被外部访问
//private 私有的 只能在内部使用
//默认不写 为private
#endregion
//变量
//结构体申明的变量 不能直接初始化
//变量类型 可以写任意类型 包括结构体 但是不能是自己的结构体
//年龄
public int age;
//性别
public bool sex;
//学号
public int number;
//姓名
public string name;
#region 结构体的构造函数
//基本概念
//1.没有返回值
//2.函数名必须和结构体名相同
//3.必须有参数
//4.如果申明了构造函数 那么必须在其中对所有变量数据初始化
//构造函数 一般是用于在外部方便初始化的
public Student(int age,bool sex,int number,string name)
{
//新的关键词 this
//代表自己
this.age = age;
this.sex = sex;
this.number = number;
this.name = name;
}
#endregion
//函数方法
//表现这个数据结构的行为
//注意 在结构体中的方法 目前不需要加 static 关键字
public void Speak()
{
Console.WriteLine("我的名字是{0},我今年{1}岁", name, age);
}
//可以根据需求 写无数个函数
}
#endregion
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
#region 结构体的使用
//变量类型 变量名;
Student s1;
s1.age = 10;
s1.sex = false;
s1.number = 1;
s1.name = "于双";
s1.Speak();
Student s2 = new Student(18, true, 2, "小红");
s2.Speak();
#endregion
}
}
//总结
//1概念:结构体 struct 是变量和函数的集合 用来表示特定的数据集合
//访问修饰符:public 和 private 用来修饰变量和方法的 public 公共的 可以被外部访问 private 私有的 只能在内部使用
//构造函数:没有返回值 函数名必须和结构体名相同 可以重载 主要帮助我们快速初始化结构体对象的
//注意:
//1.在结构体中申明的变量 不能初始化 只能在外部或者在函数中赋值(初始化)
//2.在结构体中申明的函数 不用加static
}
结构体练习题
using System;
namespace lesson12_结构体练习题
{
#region 练习题1
//使用结构体描述学员的信息,姓名,性别,年龄,班级,专业,创建两个学员对象,并对其基本信息进行初始化并打印
struct Student
{
string name;
bool sex;
int age;
int cl;
string pr;
public Student(string name, bool sex, int age, int cl, string pr)
{
this.name = name;
this.sex = sex;
this.age = age;
this.cl = cl;
this.pr = pr;
}
public void Speak()
{
Console.WriteLine("姓名:{0},性别:{1},年龄:{2},班级:{3},专业:{4}", name, sex, age, cl, pr);
}
}
#endregion
#region 练习题2
//请简要描述private和public两个关键字的区别
//public 和 private 用来修饰变量和方法的 public 公共的 可以被外部访问 private 私有的 只能在内部使用
#endregion
#region 练习题3
//使用结构体描述矩形的信息,长,宽;创建-个矩形, 对其长宽进行初始化,并打印矩形的长、宽、面积、周长等信息。
struct Rectangle
{
float Long;
float Wide;
public Rectangle(float Long, float Wide)
{
this.Long = Long;
this.Wide = Wide;
}
public void C()
{
float Area;
float Perimeter;
Area = Long * Wide;
Perimeter = 2 * (Long + Wide);
Console.WriteLine("该长方形的长为{0},宽为{1},面积为{2},周长为{3}", Long, Wide, Area, Perimeter);
}
}
#endregion
#region 练习题4
//使用结构体描述玩家信息,玩家名字,玩家职业
//请用户输入玩家姓名,选择玩家职业,最后打印玩家的攻击信息
//职业:
//战士(技能:冲锋)
//猎人(技能:假死)
//法师(技能:奥术冲击)
//打印结果:猎人唐老狮释放了假死
struct Player
{
string name;
string occ;
public Player(string name, string occ)
{
this.name = name;
this.occ = occ;
}
public void D()
{
string skill="0";
if (occ == "战士")
{
skill = "冲锋";
}
if (occ == "猎人")
{
skill = "假死";
}
if (occ == "法师")
{
skill = "奥术冲击";
}
Console.WriteLine("{0}{1}释放了{2}",name,occ,skill);
}
}
#endregion
#region 练习题5
//使用结构体描述小怪兽
class Monster
{
public string name;
public Monster(string name)
{
this.name = name;
}
}
#endregion
#region 练习题6
//定义一个数组存储10个上面描述的小怪兽,每个小怪兽的名字为(小怪兽+数组下标)
//举例:小怪兽0,最后打印10个小怪兽的名字+攻击力数值
#endregion
#region 练习题7
//应用已学过的知识,实现奥特曼打小怪兽
//提示:结构体描述奥特曼与小怪兽
//定义一个方法实现奥特曼攻击小怪兽
//定义一个方法实现小怪兽攻击奥特曼
struct OutMan
{
public string name;
public int hp;
public int atk;
public OutMan(string name, int hp, int atk)
{
this.name = name;
this.hp = hp;
this.atk = atk;
}
public void Atk()
{
Console.WriteLine("{0}奥特曼攻击了小怪兽", name);
}
}
struct Boss
{
public string name;
public int hp;
public int atk;
public Boss(string name, int hp, int atk)
{
this.name = name;
this.hp = hp;
this.atk = atk;
}
public void Atk()
{
Console.WriteLine("{0}奥特曼攻击了小怪兽", name);
}
}
#endregion
class Program
{
static void Main(string[] args)
{
Console.WriteLine("结构体练习题");
#region 练习题1
Student s1 = new Student("小明", false, 18, 4, "软件工程");
Student s2 = new Student("小红", true, 17, 4, "软件工程");
s1.Speak();
s2.Speak();
#endregion
#region 练习题3
Rectangle j1 = new Rectangle(10, 20);
Rectangle j2 = new Rectangle(5, 10);
j1.C();
j2.C();
#endregion
#region 练习题4
Console.WriteLine("请输入玩家名字和职业");
Player p1 = new Player(Console.ReadLine(), Console.ReadLine());
p1.D();
#endregion
#region 练习题7
OutMan o = new OutMan("奥特曼", 100, 10);
Boss b = new Boss("小怪兽", 150, 2);
o.Atk();
o.hp -= b.atk;
Console.WriteLine("{0}剩余血量{1}", o.name, o.hp);
b.Atk();
b.hp -= o.atk;
Console.WriteLine("{0}剩余血量{1}", b.name, b.hp);
#endregion
}
}
}
13.冒泡排序
using System;
namespace lesson13_冒泡排序
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("冒泡排序");
#region 排序的基本概念
//排序是计算机内经常进行的一种操作,其目的是将一组"无序"的记录序列调整为"有序"的记录序列
//常用的排序例子
//8 7 1 5 4 2 6 3 9
//把上面的这个无序序列 变为 有序(升序或降序)序列的过程
//1 2 3 4 5 6 7 8 9(升序)
//9 8 7 6 5 4 3 2 1(降序)
//在程序中 序列一般 存储在数组当中
//所有 排序往往是对 数组进行排序
int[] arr = new int[] { 8, 7, 1, 5, 4, 2, 6, 3, 9 };
//把数组里面的内容变为有序的
#endregion
#region 冒泡排序基本原理
//8 7 1 5 4 2 6 3 9
//两两相邻
//不停比较
//不停交换
//比较n轮
#endregion
#region 代码实现
/*for (int j = 0; j < arr.Length; j++)
{
for (int i = 0; i < arr.Length-1; i++)
{
if (arr[i] > arr[i + 1])
{
int x = 0;
x = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = x;
}
}
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(" " + arr[i]);
}*/
//优化
//1.确定位置的数字不用比较了
//确定一轮后 极值(最大或最小)已经放到了对应的位置(往后放)
//所以 每完成n轮 后面位置的数 就不用再参与比较了
/*for (int j = 0; j < arr.Length; j++)
{
for (int i = 0; i < arr.Length-1-j; i++)
{
if (arr[i] > arr[i + 1])
{
int x = 0;
x = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = x;
}
}
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(" " + arr[i]);
}*/
//特殊情况下的优化
//外面申明一个标识 来表示 该轮是否进行了交换
bool isSort = false;
for (int j = 0; j < arr.Length; j++)
{
//每一轮开始时 默认没有进行过交换
isSort = false;
for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] > arr[i + 1])
{
isSort = true;
int x = 0;
x = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = x;
}
}
//当一轮结束过后 如果isSort 这个标识 还是false
//那就意味着 已经是最终的序列了 不需要再判断交换了
if (!isSort)
{
break;
}
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(" " + arr[i]);
}
#endregion
}
}
}
冒泡排序练习题
using System;
namespace lesson13_冒泡排序练习题
{
class Program
{
#region 练习题1
//定义一个数组,长度为20,每个元素值随机0到100的数
//使用冒泡排序进行升序排序并打印
//使用冒泡排序进行降序排序并打印
static void A()
{
int[] a = new int[20];
for (int i = 0; i < a.Length; i++)
{
Random rd = new Random();
int sjs = rd.Next(1, 100);
a[i] = sjs;
}
//升序
for (int j = 0; j < a.Length; j++)
{
for (int i = 0; i < a.Length - 1 - j; i++)
{
if (a[i] > a[i + 1])
{
int x = 0;
x = a[i];
a[i] = a[i + 1];
a[i + 1] = x;
}
}
}
for (int i = 0; i < a.Length; i++)
{
Console.Write(" " + a[i]);
}
Console.WriteLine();
//逆序
for (int j = 0; j < a.Length; j++)
{
for (int i = 0; i < a.Length - 1 - j; i++)
{
if (a[i] < a[i + 1])
{
int x = 0;
x = a[i];
a[i] = a[i + 1];
a[i + 1] = x;
}
}
}
for (int i = 0; i < a.Length; i++)
{
Console.Write(" " + a[i]);
}
}
#endregion
#region 练习题2
//写一个函数,实现一个数组的排序,并返回结果。可以根据参数决定是升序还是降序
static void B(bool a)
{
int[] b = new int[20];
for (int i = 0; i < b.Length; i++)
{
Random rd = new Random();
int sjs = rd.Next(1, 100);
b[i] = sjs;
}
if (a == true)
{
for (int j = 0; j < b.Length; j++)
{
for (int i = 0; i < b.Length - 1 - j; i++)
{
if (b[i] > b[i + 1])
{
int x = 0;
x = b[i];
b[i] = b[i + 1];
b[i + 1] = x;
}
}
}
for (int i = 0; i < b.Length; i++)
{
Console.Write(" " + b[i]);
}
}
else
{
for (int j = 0; j < b.Length; j++)
{
for (int i = 0; i < b.Length - 1 - j; i++)
{
if (b[i] < b[i + 1])
{
int x = 0;
x = b[i];
b[i] = b[i + 1];
b[i + 1] = x;
}
}
}
for (int i = 0; i < b.Length; i++)
{
Console.Write(" " + b[i]);
}
}
}
#endregion
static void Main(string[] args)
{
Console.WriteLine("冒泡排序练习题");
A();
Console.WriteLine();
B(false);
}
}
}
14.选择排序
using System;
namespace lesson14_选择排序
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("选择排序");
#region 选择排序的基本原理
//8,7,1,5,4,2,6,3,9
//新建中间商
//找出极值(最大或最小)
//放入目标位置
//比较n轮
#endregion
#region 代码实现
//实现升序 把 大的 放在最后面
int[] arr = { 8, 7, 1, 5, 4, 2, 6, 3, 9 };
//比较m轮
for (int m = 0; m < arr.Length; m++)
{
//第一步 申明一个中间商 来记录索引
//每一轮开始 默认第一个都是极值
int index = 0;
//第二步
//依次比较
//减m的目的 是排除上一轮 已经放好位置的数
for (int n = 1; n < arr.Length-m; n++)
{
//第三步
//找出极值(最大值)
if (arr[index] < arr[n])
{
index = n;
}
}
//第四步 放入目标位置
//length-1-轮数
//如果当前极值所在位置 就是目标位置 那就没必要交换了
if (index != arr.Length - 1 - m)
{
int temp = arr[index];
arr[index] = arr[arr.Length - 1 - m];
arr[arr.Length - 1 - m] = temp;
}
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
#endregion
//总结
//基本概念
//新建中间商
//依次比较
//找出极值
//放入目标位置
//套路写法
//两层循环
//外层轮数
//内层寻找
//初始索引
//记录极值
//内存循环外交换
}
}
}
选择排序练习题
using System;
namespace lesson14_选择排序练习题
{
class Program
{
static void A(bool a)
{
int[] arr = new int[20];
for (int i = 0; i < arr.Length; i++)
{
Random rd = new Random();
int sjs = rd.Next(1, 100);
arr[i] = sjs;
}
if (a)
{
for (int m = 0; m < arr.Length; m++)
{
int index = 0;
for (int n = 1; n < arr.Length - m; n++)
{
if (arr[index] < arr[n])
{
index = n;
}
}
if (index != arr.Length - 1 - m)
{
int temp = arr[index];
arr[index] = arr[arr.Length-1-m];
arr[arr.Length-1-m] = temp;
}
}
}
else
{
for (int m = 0; m < arr.Length; m++)
{
int index = 0;
for (int n = 1; n < arr.Length - m; n++)
{
if (arr[index] > arr[n])
{
index = n;
}
}
if (index != arr.Length - 1 - m)
{
int temp = arr[index];
arr[index] = arr[arr.Length - 1 - m];
arr[arr.Length - 1 - m] = temp;
}
}
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
}
static void Main(string[] args)
{
Console.WriteLine("选择排序练习题");
A(true);
}
}
}