【发布时间】:2017-09-10 21:04:53
【问题描述】:
这里是学生。
正在处理当前的 C# 项目。我必须创建一个扑克牌列表,以及一个 Dictionary> >。
列表是独立创建的,字典包含卡片的集合。可以从集合中添加或删除卡片。
不过,我在将卡片添加到收藏中时遇到了问题。我从用户输入中获得了列表的索引号,并试图根据索引简单地更新字典中的值。
抛出错误的行在这里。错误是
无法将类型“
我一生都无法弄清楚这意味着什么以及如何解决它..
_collections[input] = _cards[cardSelectionIndex];
这是我当前的代码块:
public void AddToCollection()
{
PrintValuesCollections<string, List<Card>>(_collections);
Console.WriteLine("Type in the name of the collection to add a card to:");
string input = Console.ReadLine();
bool found = _collections.ContainsKey(input);
if (found)
{
Console.WriteLine("List of current cards to choose from:");
for (int i = 0; i < _cards.Count; i++)
{
Console.WriteLine("Index number:\r\n"+i+"Card Info:\r\n"+_cards[i]+"\n");
}
Console.WriteLine("Type in the index of the card from the list to add to the collection:");
string cardSelection = Console.ReadLine();
int cardSelectionIndex = 0;
while (!int.TryParse(cardSelection, out cardSelectionIndex))
{
Console.Write("Please enter a number from the list above: ");
input = Console.ReadLine();
}
_collections[input] = _cards[cardSelectionIndex];
_cards.RemoveAt(cardSelectionIndex);
}
else
Console.WriteLine("Collection name not found.");
}
【问题讨论】:
-
_collections[input].Add(_cards[cardSelectionIndex]);? -
我不确定,但这不会添加一个新的唯一键吗?我之前应该提到过,我有已经创建的键,只是想根据用户的键输入来更新值。
标签: c# list dictionary