Home [C# 筆記] Hashtable 集合練習
Post
Cancel

[C# 筆記] Hashtable 集合練習

練習:將用戶輸入的繁體字轉換成簡體字

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
using System.Collections;

private const string traditional = "您好啊很高興認識你"; //繁體
private const string simplified = "您好啊很高兴认识你"; //簡體

Console.WriteLine("請輸入您想要轉換的繁體字:");
string input = Console.ReadLine()!; //接收用戶輸入

//建立hashtable物件
Hashtable ht = new Hashtable();
//將每一個字添加到hashtable裡
for (int i = 0; i < traditional.Length; i++)
{
    ht.Add(traditional[i], simplified[i]); //key:value (繁:簡)
}

//遍歷每個輸入的字
for (int i = 0; i < input.Length; i++)
{
    //檢查hashtable裡有沒有input[i]的key值
    if (ht.ContainsKey(input[i]))
    {   //有就輸出對應的值
        Console.Write(ht[input[i]]);
    } else
    {   //沒有就輸出初值
        Console.Write(input[i]);
    }
}
This post is licensed under CC BY 4.0 by the author.