From c86de4e6be12c304297da6e26dd9b3b69fbefa71 Mon Sep 17 00:00:00 2001 From: Kister Hakusan <2753888203@qq.com> Date: Tue, 14 Oct 2025 16:07:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Lesson8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- C#进阶/C#进阶.sln | 12 +++ .../Lesson6_Dictionary练习.csproj | 10 ++ C#进阶/Lesson6_Dictionary练习/Program.cs | 10 ++ .../Lesson8_Dictionary.csproj | 10 ++ C#进阶/Lesson8_Dictionary/Program.cs | 89 +++++++++++++++++ .../Lesson8_Dictionary练习.csproj | 10 ++ C#进阶/Lesson8_Dictionary练习/Program.cs | 99 +++++++++++++++++++ 7 files changed, 240 insertions(+) create mode 100644 C#进阶/Lesson6_Dictionary练习/Lesson6_Dictionary练习.csproj create mode 100644 C#进阶/Lesson6_Dictionary练习/Program.cs create mode 100644 C#进阶/Lesson8_Dictionary/Lesson8_Dictionary.csproj create mode 100644 C#进阶/Lesson8_Dictionary/Program.cs create mode 100644 C#进阶/Lesson8_Dictionary练习/Lesson8_Dictionary练习.csproj create mode 100644 C#进阶/Lesson8_Dictionary练习/Program.cs diff --git a/C#进阶/C#进阶.sln b/C#进阶/C#进阶.sln index 7c6ef30..2826e8d 100644 --- a/C#进阶/C#进阶.sln +++ b/C#进阶/C#进阶.sln @@ -33,6 +33,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson7_List", "Lesson7_Lis EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson7_List练习", "Lesson7_List练习\Lesson7_List练习.csproj", "{1E65491F-EA06-4E4D-9E20-A692C9EB3C79}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson8_Dictionary", "Lesson8_Dictionary\Lesson8_Dictionary.csproj", "{42A1E4A3-FE66-4E6A-B2C0-D383BDADD315}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson8_Dictionary练习", "Lesson8_Dictionary练习\Lesson8_Dictionary练习.csproj", "{1481414C-D336-4691-A45A-6A8CABFD33FE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -99,6 +103,14 @@ Global {1E65491F-EA06-4E4D-9E20-A692C9EB3C79}.Debug|Any CPU.Build.0 = Debug|Any CPU {1E65491F-EA06-4E4D-9E20-A692C9EB3C79}.Release|Any CPU.ActiveCfg = Release|Any CPU {1E65491F-EA06-4E4D-9E20-A692C9EB3C79}.Release|Any CPU.Build.0 = Release|Any CPU + {42A1E4A3-FE66-4E6A-B2C0-D383BDADD315}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {42A1E4A3-FE66-4E6A-B2C0-D383BDADD315}.Debug|Any CPU.Build.0 = Debug|Any CPU + {42A1E4A3-FE66-4E6A-B2C0-D383BDADD315}.Release|Any CPU.ActiveCfg = Release|Any CPU + {42A1E4A3-FE66-4E6A-B2C0-D383BDADD315}.Release|Any CPU.Build.0 = Release|Any CPU + {1481414C-D336-4691-A45A-6A8CABFD33FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1481414C-D336-4691-A45A-6A8CABFD33FE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1481414C-D336-4691-A45A-6A8CABFD33FE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1481414C-D336-4691-A45A-6A8CABFD33FE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/C#进阶/Lesson6_Dictionary练习/Lesson6_Dictionary练习.csproj b/C#进阶/Lesson6_Dictionary练习/Lesson6_Dictionary练习.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson6_Dictionary练习/Lesson6_Dictionary练习.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson6_Dictionary练习/Program.cs b/C#进阶/Lesson6_Dictionary练习/Program.cs new file mode 100644 index 0000000..08d14a2 --- /dev/null +++ b/C#进阶/Lesson6_Dictionary练习/Program.cs @@ -0,0 +1,10 @@ +namespace Lesson6_Dictionary练习 +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } + } +} diff --git a/C#进阶/Lesson8_Dictionary/Lesson8_Dictionary.csproj b/C#进阶/Lesson8_Dictionary/Lesson8_Dictionary.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson8_Dictionary/Lesson8_Dictionary.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson8_Dictionary/Program.cs b/C#进阶/Lesson8_Dictionary/Program.cs new file mode 100644 index 0000000..bf21b06 --- /dev/null +++ b/C#进阶/Lesson8_Dictionary/Program.cs @@ -0,0 +1,89 @@ +using System.Collections.Generic; +namespace Lesson8_Dictionary +{ + internal class Program + { + static void Main(string[] args) + { + #region 知识点一 Dictionary本质 + //可以将Dictionary理解为 拥有泛型的Hashtable + //他也是极于剑的哈希代码组织起来的 键/值对 + //键值对类型从Hashtable的object变为了可以自己指定的泛型 + #endregion + + #region 知识点二 声明 + Dictionary dictionary = new Dictionary(); + #endregion + + #region 知识点三 增删查改 + #region 增 + //注意:不能出现相同键 + dictionary.Add(1, "111"); + dictionary.Add(2, "222"); + dictionary.Add(3, "333"); + #endregion + + #region 删 + //1.只能通过键去删除 + // 删除不存在的键 无反应 + dictionary.Remove(1); + dictionary.Remove(4); + //2.清空 + dictionary.Clear(); + #endregion + + #region 查 + //1.通过键查看值 + //注意:找不到直接报错,而不是像hashtable一样无反应 + dictionary.Add(1, "111"); + dictionary.Add(2, "222"); + dictionary.Add(3, "333"); + Console.WriteLine(dictionary[2]);//"222" + //Console.WriteLine(dictionary[4]);//不会像hashtable一样无反应而是直接报错 + //2.查看是否存在 + // 根据键检测 + if (dictionary.ContainsKey(1) ) + { + Console.WriteLine("存在键为1的键值对"); + } + // 根据值检测 + if (dictionary.ContainsValue("111")) + { + Console.WriteLine("存在值为111的键值对"); + } + #endregion + + #region 改 + Console.WriteLine(dictionary[1]); + dictionary[1] = "999"; + Console.WriteLine(dictionary[1]); + #endregion + #endregion + + #region 知识点四 遍历啊 + Console.WriteLine("---------------------------"); + //长度 + Console.WriteLine(dictionary.Count); + //1.遍历所有键 + Console.WriteLine("---------------------------"); + foreach (int item in dictionary.Keys) + { + Console.WriteLine(item); + Console.WriteLine(dictionary[item]); + } + //2.遍历所有值 + Console.WriteLine("---------------------------"); + foreach (string item in dictionary.Values) + { + Console.WriteLine(item); + } + //3.键值对一起遍历 + Console.WriteLine("---------------------------"); + foreach (KeyValuePair pair in dictionary) + { + Console.WriteLine($"键:{pair.Key} 值:{pair.Value}"); + } + #endregion + } + } +} diff --git a/C#进阶/Lesson8_Dictionary练习/Lesson8_Dictionary练习.csproj b/C#进阶/Lesson8_Dictionary练习/Lesson8_Dictionary练习.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson8_Dictionary练习/Lesson8_Dictionary练习.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson8_Dictionary练习/Program.cs b/C#进阶/Lesson8_Dictionary练习/Program.cs new file mode 100644 index 0000000..d3ec8bc --- /dev/null +++ b/C#进阶/Lesson8_Dictionary练习/Program.cs @@ -0,0 +1,99 @@ +namespace Lesson8_Dictionary练习 +{ + class Num + { + public static Dictionary num = new Dictionary(); + + // 使用静态构造函数初始化字典 + static Num() + { + num.Add(1, "壹"); + num.Add(2, "贰"); + num.Add(3, "叁"); + num.Add(4, "肆"); + num.Add(5, "伍"); + num.Add(6, "陆"); + num.Add(7, "柒"); + num.Add(8, "捌"); + num.Add(9, "玖"); + num.Add(0, "零"); + } + + public static void RerunNum(int number) + { + if (number > 999 || number < 0) + { + Console.WriteLine("越界"); + return; + } + + if (number == 0) + { + Console.WriteLine("零"); + return; + } + + string result = ""; + + // 处理百位 + int hundreds = number / 100; + if (hundreds > 0) + { + result += num[hundreds] + "佰"; + } + + // 处理十位 + int tens = (number % 100) / 10; + if (tens > 0) + { + result += num[tens] + "拾"; + } + else if (hundreds > 0 && number % 100 > 0) + { + // 百位不为0,十位为0,个位不为0时加"零" + result += "零"; + } + + // 处理个位 + int ones = number % 10; + if (ones > 0) + { + result += num[ones]; + } + + Console.WriteLine(result); + } + } + + class Program + { + static void Main(string[] args) + { + Console.WriteLine("请输入一个数"); + Num.RerunNum(int.Parse(Console.ReadLine())); + + + + + Dictionary dic = new Dictionary(); + string str = "Welcome to Unity World!"; + str = str.ToLower(); + for (int i = 0; i < str.Length; i++) + { + if (dic.ContainsKey(str[i])) + { + dic[str[i]] += 1; + } + else + { + dic.Add(str[i], 1); + } + } + foreach (var item in dic) + { + Console.WriteLine($"{item.Key}字母出现了{item.Value}次"); + } + } + + } +} \ No newline at end of file