C#基础篇五值类型和引用类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | 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 } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | 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 } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?