Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# &引用;隐式转换类型';System.Generic.Collection.IEnumerable<;int>;至<;int>;_C#_Linq_Collections_Ienumerable_Key Value - Fatal编程技术网

C# &引用;隐式转换类型';System.Generic.Collection.IEnumerable<;int>;至<;int>;

C# &引用;隐式转换类型';System.Generic.Collection.IEnumerable<;int>;至<;int>;,c#,linq,collections,ienumerable,key-value,C#,Linq,Collections,Ienumerable,Key Value,我正在建立一个程序,将采取两个用户选择的卡从一个标准的卡牌组。甲板由KeyValuePairs组成,其中列出了编号,然后是套装。用户选择两张卡后,我需要程序将卡的整数部分相加。我不知道如何获取KeyValuePair的关键部分 我知道我下面的代码显然不起作用,但我已经包含了这张图片来显示我正在尝试做什么。我的程序应该从“所选卡片索引”的KeyValuePairs中获取键值,相反,它在这篇文章的标题中提供了错误 我曾尝试使用convert.ToInt32将整行转换为字符串,但也不起作用。我到处寻

我正在建立一个程序,将采取两个用户选择的卡从一个标准的卡牌组。甲板由KeyValuePairs组成,其中列出了编号,然后是套装。用户选择两张卡后,我需要程序将卡的整数部分相加。我不知道如何获取KeyValuePair的关键部分

我知道我下面的代码显然不起作用,但我已经包含了这张图片来显示我正在尝试做什么。我的程序应该从“所选卡片索引”的KeyValuePairs中获取键值,相反,它在这篇文章的标题中提供了错误

我曾尝试使用convert.ToInt32将整行转换为字符串,但也不起作用。我到处寻找这个问题的精确答案,但运气不佳

以下是所有其他相关代码:

    public static List<KeyValuePair<int, string>> Player1Hand = new 
    List<KeyValuePair<int, string>>(10); // creating the player's hand

    public static int chosencard;
    public static int player1total;
    public static string input;

    input = Console.ReadLine();
    chosencard2 = Int32.Parse(input);

    player1total = Hand.Player1Hand[chosencard];
    Console.WriteLine("Player 1's total is " + player1total);

public static List Player1Hand=new
名单(10);//创建玩家的手
公共静态int chosencard;
公共静态整数播放器1总;
公共静态字符串输入;
input=Console.ReadLine();
chosencard2=Int32.Parse(输入);
player1total=手牌。Player1Hand[chosencard];
Console.WriteLine(“玩家1的总数为”+玩家1的总数);

谢谢大家的帮助。

您的
Player1Hand
是一个
KeyValue
列表。因此,您当前正在访问该对,而您应该在给定索引处访问该对的密钥

我想,根据你给我的信息,这应该可以做到:

player1Total += Hand.Player1Hand[chosencard].Key

根据您发布的图片,您有一个
列表
,并使用该列表访问元素。作为返回类型,您将获得一个
KeyValuePair
,当然不能将其强制转换为
int
。根据您想要实现的目标,您可以访问
KeyValuePair
的或。根据类型,我会猜测没有任何代码的

,我们能做的最好的事情就是猜测,我们在这里不这么做。因此,请提供一些代码,以便我们可以看到什么是错误的。链接图片是不够的。既然你可以使用字典,为什么还要使用
列表呢?。如果你做了
Dictionary Player1Hand
你就可以
Player1Hand.Keys
对不起,这里是新的,我已经在帖子中添加了所有相关的代码。为抬头的人干杯。