C#基础篇五值类型和引用类型

时间:2023-03-08 16:58:42
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace P01Method
{
class Program
{
static void Main(string[] args)
{
//int a1 = 11;
//int b2 = 22;
//Add2Num(a1, b2);//在调用方法时,为 方法括号中 传递的 值 就叫做 实参(实际参数) //Add2Num(102, 205);//在调用方式时,也可以 直接 用 把值 传递个 方法的 形参 int res = Add2NumWith(222, 555);//使用 res 变量 接收 方法的返回值
Console.WriteLine("返回值为:" + res);
M01Review();
Console.ReadLine();
} #region 2.2 返回值: 计算两个 数值 的 和,并返回给方法的调用者
/// <summary>
/// 2.2 计算两个 数值 的 和
/// </summary>
static int Add2NumWith(int a, int b)//括号中的 “变量”,叫 形参(形式参数):本质就是 方法的局部变量
{
int x = a + b;
return x;//将计算的 结果 返回给 方法 的 调用
}
#endregion #region 2.1 形参:计算两个 数值 的 和
/// <summary>
/// 2.1 计算两个 数值 的 和
/// </summary>
static void Add2Num(int a,int b)//括号中的 “变量”,叫 形参(形式参数):本质就是 方法的局部变量
{
int x = a + b;
Console.WriteLine(x);
}
#endregion #region 1.0 方法(第一个方法)
/// <summary>
/// 1.0 方法(第一个方法)
/// 方法的好处: 复用代码-方便调用 和 统一修改
/// 减少代码的冗余(重复的代码)
/// 封装代码:管理业务思路,将不同业务的代码 分开!
/// </summary>
static void ShowLogo()
{
Console.WriteLine("*****************");
Console.WriteLine("* 小白 *");
Console.WriteLine("* I love u *");
Console.WriteLine("*****************");
}
#endregion #region 0.2 值类型和引用类型
/// <summary>
/// 0.2 值类型和引用类型
/// </summary>
static void M02StatckAndHeap()
{
int a = 110;
int b = a;
b = 112;
Console.WriteLine("a=" + a);//110 int[] arr = new int[1];
arr[0] = 220;
int[] arr2 = arr;
arr2[0] = 212; Console.WriteLine("arr[0]=" + arr[0]);//212
}
#endregion #region 0.1 复习 goto M01Review()
/// <summary>
/// 0.1 复习 goto
/// </summary>
static void M01Review()
{
//goto 语句的标签:
sss:
Console.WriteLine("我在循环之前哦~~~!"); for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
if (row == 0 && col == 1)
{
//1.通过满足外部循环条件 的方式 退出 外部循环
//row = 100;
//2.goto语句 退出 外部循环
goto end;//直接 根据 标签名 跳到 标签后的代码 执行
break;
}
Console.Write("*");
}
Console.Write("\n");
} //goto 语句的标签:
end:
Console.WriteLine("");
}
#endregion
}
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace P02StuManage
{
class Program
{
//------------0. 定义静态的全局变量(类成员变量)stuCount ,arrID ,arrName ,arrAge ,arrSex---------------
#region 0.0 定义静态的全局变量(类成员变量) stuCount ,arrID ,arrName ,arrAge ,arrSex
/// <summary>
/// 学员数量
/// </summary>
static int stuCount = 0; /// <summary>
/// 学员ID种子,作为新增学员时,自动生成ID号的 标志
/// </summary>
static int stuIdSeed = 1; /// <summary>
/// 0.1 保存所有学员的 id 数组
/// </summary>
static int[] arrID = new int[2]; /// <summary>
/// 0.2 保存所有学员的 名字 的数组
/// </summary>
static string[] arrName = new string[2]; /// <summary>
/// 0.3 保存所有学员的 年龄 的数组
/// </summary>
static int[] arrAge = new int[2]; /// <summary>
/// 0.4 保存所有学员的 性别 的数组
/// </summary>
static bool[] arrSex = new bool[2];
#endregion static void Main(string[] args)
{
do
{
Console.Clear();//清空屏幕
// 方法练习:学员管理
//1.询问用户要进行的操作,需要写一个方法,接收用户的操作
int usrOpeType = GetUserOperation();
//2.根据用户操作选择,执行操作业务
OperateByOneLevelType(usrOpeType);
//3.询问用户是否要继续操作
Console.WriteLine("操作已结束,您是否要继续操作呢?(y/n)");
} while (Console.ReadLine() == "y"); Console.ReadLine();
} //--------------------------------------1.0 程序开始操作级别的 代码------------------------------------
#region 1.0 获取用户操作 int GetUserOperation()
/// <summary>
/// 1.0 获取用户操作
/// </summary>
/// <returns>返回用户选择的操作类型(1-4的一个数值)</returns>
static int GetUserOperation()
{
do
{
Console.WriteLine("欢迎使用16期基础班学员管理系统");
Console.WriteLine("请选择您要进行的操作(1-4):");
Console.WriteLine("1.新增学员");
Console.WriteLine("2.删除学员");
Console.WriteLine("3.修改学员");
Console.WriteLine("4.查询学员"); string strOpeType = Console.ReadLine();//获取用户的操作选择
int opeType = 0;
//1.1检查 用户输入 是否是 1-4 之间的数值!
if (int.TryParse(strOpeType, out opeType) && (opeType >= 1 && opeType <= 4))
{
//1.2执行操作
return opeType;//将用户选择的操作 返回 给方法的调用,【注意:return后,方法结束运行!(循环也就不执行了!)】
}
else//1.3不通过,要求用户重新输入
{
Console.WriteLine("您的输入必须是1-4之间的数值,请重新选择~");
}
} while (true);
}
#endregion #region 2.0 接收用户选择的 一级 操作选择,并根据 选择,执行不同的业务 void OperateByType(int opeType)
/// <summary>
/// 2.0 接收用户选择的 一级 操作选择,并根据 选择,执行不同的业务
/// </summary>
/// <param name="opeType">用户一级操作选择(1-4)</param>
static void OperateByOneLevelType(int opeType)
{
switch (opeType)
{
case 1://新增学员
{
AddStu();
break;
}
case 2://删除学员
{
break;
}
case 3://修改学员
{
break;
}
case 4://查询学员
{
ChooseQueryType();
break;
}
default:
{
break;
}
}
}
#endregion //--------------------------------------3.0进行各项业务操作的方法------------------------------------ //--------------------------------------3.1新增业务------------------------------------
#region 3.1要能添加学员(ID,名字,年龄,性别)void AddStu()
/// <summary>
/// 3.1要能添加学员(ID,名字,年龄,性别)
/// </summary>
static void AddStu()
{
//1.接收用户的信息
Console.Write("请输入学员的名字:");
string strStuName = Console.ReadLine(); Console.Write("请输入学员的年龄:");
string strStuAge = Console.ReadLine(); Console.Write("请输入学员的性别:(男/女)");
string strStuSex = Console.ReadLine(); //2.将用户输入的信息 分别存入 不同的数组中!
//2.1问题.数组中是否已经存满了呢?如果满了,需要扩容(创建一个新的更大的数组来存放数据)
if (stuCount >= arrID.Length)//如果 学员的个数 已经 满了,就需要 【扩容】
{
//a.为 4 个数组 做扩容,创建 老数组长度 的 2倍 的新数组
int[] arrIDTemp = new int[arrID.Length * 2];
string[] arrNameTemp = new string[arrID.Length * 2];
int[] arrAgeTemp = new int[arrID.Length * 2];
bool[] arrSexTemp = new bool[arrID.Length * 2];
//b.将老数组里的数据 复制到 新数组中
arrID.CopyTo(arrIDTemp, 0);
arrName.CopyTo(arrNameTemp, 0);
arrAge.CopyTo(arrAgeTemp, 0);
arrSex.CopyTo(arrSexTemp, 0);
//c.将新数组对象 覆盖 老数组
arrID = arrIDTemp;
arrName = arrNameTemp;
arrAge = arrAgeTemp;
arrSex = arrSexTemp;
} //3.将新学员数据 根据 已添加的学员人数,放在 数组 对应的位置
arrID[stuCount] = stuIdSeed++;//学员ID自动生成,注意:先执行 =号,再 ++
arrName[stuCount] = strStuName;
arrAge[stuCount] = int.Parse(strStuAge);
arrSex[stuCount] = strStuSex == "男" ? true : false; //4.学员总数加1
stuCount++; Console.WriteLine("新增完毕~~");
}
#endregion //--------------------------------------3.2删除业务------------------------------------
#region 3.2根据 ID 删除 void RemoveStuById()
/// <summary>
/// 3.2要能移除学员(根据【ID】移除学员/根据【名字】移除学员)
/// </summary>
static void RemoveStuById()
{
}
#endregion #region 3.3 根据 姓名 删除 void RemoveStuByName()
/// <summary>
/// 3.3 根据 姓名 删除
/// </summary>
static void RemoveStuByName()
{
}
#endregion //--------------------------------------3.4修改业务------------------------------------
#region 3.4要能修改学员(除了 ID 不能改,其它都能改) void ModifyStu()
/// <summary>
/// 3.4要能修改学员(除了 ID 不能改,其它都能改)
/// </summary>
static void ModifyStu()
{
}
#endregion //--------------------------------------3.4 查询业务 要能根据条件查询学员(根据 ID/名字/年龄段/性别 单条件查询------------------------------------ #region 3.4 根据用户的选择,使用不同的查询方式 void ChooseQueryType()
/// <summary>
/// 3.4 根据用户的选择,使用不同的查询方式
/// </summary>
static void ChooseQueryType()
{
do
{
Console.WriteLine("请输入您要进行的查询方式(1-4):");
Console.WriteLine("1.根据【学员ID】查询");
Console.WriteLine("2.根据【学员名称】查询");
Console.WriteLine("3.根据【年龄段】查询");
Console.WriteLine("4.根据【学员性别】查询");
string strOpeType = Console.ReadLine();
int opeType = 0; if (int.TryParse(strOpeType, out opeType))
{
switch (opeType)
{
case 1://根据【学员ID】查询
{
QueryStuById();
break;
}
case 2://根据【学员名称】查询
{
QueryStuByName();
break;
}
case 3://根据【年龄段】查询
{
QueryStuByAge();
break;
}
case 4://根据【学员性别】查询
{
QueryStuBySex();
break;
}
} break;
}
else
{
Console.WriteLine("请输入1-4之间的数值~!");
}
} while (true);
}
#endregion #region 3.4.1 根据【学员id】查询 void QueryStuById()
/// <summary>
/// 3.4.1 根据【学员id】查询
/// </summary>
static void QueryStuById()
{
int stuId = 0;
while (true)
{
Console.WriteLine("请输入您要查询的【学员ID】");
string strStuId = Console.ReadLine();
if (int.TryParse(strStuId, out stuId))
{
break;
}
}
//1.根据id到 学员id数组 arrID 中 查找,看是否存在 相同的 ID
//根据 学员数量 循环 查找 id数组里是否有相同的id
for (int i = 0; i < stuCount; i++)
{
if (arrID[i] == stuId)
{
ShowStuMsgByIndex(i);
break;
}
}
}
#endregion #region 3.4.2 根据【学员名称】 void QueryStuByName()
/// <summary>
/// 3.4.2 根据【学员名称】
/// </summary>
static void QueryStuByName()
{
Console.WriteLine("请输入您要查询的【学员名称】");
string strName = Console.ReadLine();
//1.根据id到 学员id数组 arrID 中 查找,看是否存在 相同的 ID
//根据 学员数量 循环 查找 id数组里是否有相同的id
for (int i = 0; i < stuCount; i++)
{
if (arrName[i] == strName)
{
ShowStuMsgByIndex(i);
break;
}
}
}
#endregion #region 3.4.3 根据【年龄段】查询 void QueryStuByAge()
/// <summary>
/// 3.4.3 根据【年龄段】查询
/// </summary>
static void QueryStuByAge()
{
Console.WriteLine("请输入 开始年龄:");
int ageBegin = int.Parse(Console.ReadLine());
Console.WriteLine("请输入 结束年龄:");
int ageEnd = int.Parse(Console.ReadLine());
if (ageBegin > ageEnd)
{
Console.WriteLine("开始年龄必须 小于 或 等于 结束年龄");
}
else
{
for (int i = 0; i < stuCount; i++)
{
//如果找到 年龄 在要求的 年龄段 之间,则显示学员
if (arrAge[i] >= ageBegin && arrAge[i] <= ageEnd)
{
ShowStuMsgByIndex(i);
}
}
}
}
#endregion #region 3.4.4 根据性别查询 void QueryStuBySex()
/// <summary>
/// 3.4.4 根据性别查询
/// </summary>
static void QueryStuBySex()
{
Console.WriteLine("请输入您要查询的【学员性别】(男/女)");
bool boolGender = Console.ReadLine() == "男";// ? true : false; //1.根据 性别 到 学员 性别数组 arrID 中 查找,看是否存在 相同的 性别 学员
for (int i = 0; i < stuCount; i++)
{
if (arrSex[i] == boolGender)
{
ShowStuMsgByIndex(i);
}
}
}
#endregion //------------------------------ #region 9.0 根据学员 下标 ,显示 在4个数组中 保存的 对应学员信息 void ShowStuMsgByIndex(int stuIndex)
/// <summary>
/// 9.0 根据学员 下标 ,显示 在4个数组中 保存的 对应学员信息
/// </summary>
/// <param name="stuIndex"></param>
static void ShowStuMsgByIndex(int stuIndex)
{
int stuID = arrID[stuIndex];
string stuName = arrName[stuIndex];
int stuAge = arrAge[stuIndex];
string strSex = arrSex[stuIndex] ? "男" : "女"; string strMsg = string.Format("学员ID:{0} - 学员名称:{1} - 学员年龄:{2} - 学员性别:{3}", stuID, stuName, stuAge, strSex);
Console.WriteLine(strMsg);
}
#endregion
}
}

  C#基础篇五值类型和引用类型

C#基础篇五值类型和引用类型

C#基础篇五值类型和引用类型