添加Lesson8

This commit is contained in:
2025-10-14 16:07:14 +08:00
parent 3c4123d514
commit c86de4e6be
7 changed files with 240 additions and 0 deletions
+12
View File
@@ -33,6 +33,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson7_List", "Lesson7_Lis
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson7_List练习", "Lesson7_List练习\Lesson7_List练习.csproj", "{1E65491F-EA06-4E4D-9E20-A692C9EB3C79}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson7_List练习", "Lesson7_List练习\Lesson7_List练习.csproj", "{1E65491F-EA06-4E4D-9E20-A692C9EB3C79}"
EndProject 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 Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{1E65491F-EA06-4E4D-9E20-A692C9EB3C79}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,10 @@
namespace Lesson6_Dictionary练习
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+89
View File
@@ -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<int, string> dictionary = new Dictionary<int, string>();
#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<int, string> pair in dictionary)
{
Console.WriteLine($"键:{pair.Key} 值:{pair.Value}");
}
#endregion
}
}
}
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,99 @@
namespace Lesson8_Dictionary练习
{
class Num
{
public static Dictionary<int, string> num = new Dictionary<int, string>();
// 使用静态构造函数初始化字典
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<char, int> dic = new Dictionary<char, int>();
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}次");
}
}
}
}