99 lines
2.4 KiB
C#
99 lines
2.4 KiB
C#
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}次");
|
|
}
|
|
}
|
|
|
|
}
|
|
} |