【发布时间】:2021-07-03 15:26:04
【问题描述】:
你好。
我正在尝试自己学习升 C,但我在将输入作为角色输入时遇到了问题。 我的代码部分是:
var foodTypeMap = new Dictionary<string, string>();
foodTypeMap["1"] = "Soups";
foodTypeMap["2"] = "Vegetables";
foodTypeMap["3"] = "Mains";
foodTypeMap["4"] = "Deserts";
...
...
string fType = Console.ReadLine();
string dishType = " ";
//if else statements here.. etc
dishType = foodTypeMap[fType];
通过这种方式,我可以将 fType 获取为整数并以此初始化 discType。我想通过字符来选择,比如 x 代表汤,q 代表沙漠。我试过了
Using Console.ReadLine()[0]
Using Console.ReadKey().KeyChar
Using Char.TryParse()
Using Convert.ToChar()
但是来不及了。有没有人可以帮助我理解这一点?
【问题讨论】:
-
一般来说,
ftype[0]会给你字符串的第一个字符。您只说“无法做到”,我们需要知道当您尝试使用它时发生了什么来告诉您如何改进它。您是否修改了字典以使x实际上 表示汤?因为现在1表示汤,而不是x。 -
@LasseV.Karlsen 当您说修复字典时,我得到了问题.. 这对我来说已经足够了。谢谢:D