我正在尝试使用字典进行c#测验,该字典随机选择我字典中的一项。
我收到2条错误行33,38,我无法将字符串转换为int。
我不知道如何修正我的代码,因此如果有人可以告诉我该怎么做或我做错了什么,我将不胜感激。
这是我的代码
namespace RNG_QUIZ
{
class Program
{
static public void Main()
{
//maak een dictionary aan
Console.WriteLine("Quiz");
Dictionary<int, string> Questions =
new Dictionary<int, string>();
//voeg vragen toe
//key koppelen aan vraag
Questions.Add(11, "Vraag1");
Questions.Add(12, "Vraag2");
Questions.Add(13, "Vraag3");
Questions.Add(14, "Vraag4");
Questions.Add(15, "Vraag5");
foreach (KeyValuePair<int, string> ele1 in Questions)
{
Console.WriteLine("{0} and {1}",
ele1.Key, ele1.Value);
}
List<string> keylist = new List<string>(Questions.Keys); //error cannot convert from 'System.Collections.Generic.Dictionary<int, string>.KeyCollection' to 'int'
Random rand = new Random();
string randomKey = keylist[rand.Next(keylist.Count)];
return Questions[randomKey]; //error cant convert from 'string' to int
Console.ReadKey();
}
}
}
答案 0 :(得分:1)
第一个错误
您的
Dictionary<int,string>
的密钥是一个整数,因此return Questions[randomKey];
是一个问题,因为randomKey
被声明为string
。
第二个错误
List<string> keylist = new List<string>(Questions.Keys); //error
这是一个问题,因为
cannot convert from 'System.Collections.Generic.Dictionary<int, string>.KeyCollection' to 'int'Questions.Keys
是Dictionary<int,string>.KeyCollection
,而您 正在尝试将其分配给List<int>
。但是,您可以这样做var p = Questions.Keys.ToList();
和p将是List<int>
。
回到您想要的位置,您想要从Dictionary
中随机选择一个键并将其设置为要显示的问题,因此在您的示例中,您想要从组{11,中选择一个随机整数, 12,13,14,15},您可以像这样
List<int>
中选择一个随机数
var randomKey = Questions.Keys.OrderBy(x => rand.Next()).First();
//Here Questions.Keys is a List<int>
获取随机数的另一种方法是@Ruchin Munjal的其他海报-
var keylist = new List<int>();
keylist.AddRange(Questions.Keys);
var next = rand.Next(keylist.Count);
var randomKey = keylist[next];
全部包装
Console.WriteLine("Quiz");
Dictionary<int, string> Questions =
new Dictionary<int, string>();
//voeg vragen toe
//key koppelen aan vraag
Questions.Add(11, "Vraag1");
Questions.Add(12, "Vraag2");
Questions.Add(13, "Vraag3");
Questions.Add(14, "Vraag4");
Questions.Add(15, "Vraag5");
foreach (KeyValuePair<int, string> ele1 in Questions)
{
Console.WriteLine("{0} and {1}",
ele1.Key, ele1.Value);
}
Random rand = new Random();
int randomKey = Questions.Keys.OrderBy(x => rand.Next()).First();
Console.WriteLine(Questions[randomKey]);
Console.ReadKey();
答案 1 :(得分:0)
要完成这项工作,您需要更改一些内容:
int
密钥保存到List<string>
中,需要声明List<>
来键入int
您正在将Questions.Keys
作为参数提供给List<>
的构造函数,要解决此问题,只需不给
构造函数,然后保存调用ToList()
的密钥
方法。
List<int> keylist = new List<int>(); // they key list should be int, since keys are declared as integers in your dictionary
keylist = Questions.Keys.ToList();
获取由问题数量界定的随机索引比获取最低和最高键值要容易得多, 您可以这样做:
Random rand = new Random();
int countKeys = Questions.Keys.Count(); //get number of questions
int randomIndex = rand.Next(0,countKeys); // get a random index from 0 to number of questions
您在主要方法(即return
)中使用void
。如果您要打印的是结果,请使用
Console.WriteLine()
最后但并非最不重要的一点,就是按索引获取字典键,您只需使用.ElementAt()
。
就是这样,将所有内容放在一起看起来像这样:
//maak een dictionary aan
Console.WriteLine("Quiz");
Dictionary<int, string> Questions =
new Dictionary<int, string>();
//voeg vragen toe
//key koppelen aan vraag
Questions.Add(11, "Vraag1");
Questions.Add(12, "Vraag2");
Questions.Add(13, "Vraag3");
Questions.Add(14, "Vraag4");
Questions.Add(15, "Vraag5");
foreach (KeyValuePair<int, string> ele1 in Questions)
{
Console.WriteLine("{0} and {1}",
ele1.Key, ele1.Value);
}
List<int> keylist = new List<int>(); // they key list should be int, since keys are declared as integers in your dictionary
keylist = Questions.Keys.ToList();
Random rand = new Random();
int countKeys = Questions.Keys.Count();
int randomIndex = rand.Next(0,countKeys);
Console.WriteLine(Questions.ElementAt(randomIndex).Key); //here you are giving a random index and getting the key that is holding that index
Console.ReadKey();
答案 2 :(得分:0)
尝试将第一个错误的行更改为
List<int> keylist = new List<int>(Questions.Select(x => x.Key));
答案 3 :(得分:0)
C#中的列表是强类型的。这意味着,如果您正在创建strings
的列表,则无法添加/插入int
。您的字典的类型为<int,string>
,这意味着您的键将为int
,值将为string
。您声明了string
的列表,并试图插入类型为int
的键
dotnet错误通常是非常可解释的。
您需要按以下方式更改代码
var keylist = new List<int>();
keylist.AddRange(Questions.Keys);
第二个错误也具有与上述相同的问题。您可以将其更改为
Random rand = new Random();
var next= rand.Next(keylist.Count);
var randomKey = keylist[next]; //note randomKey is int
答案 4 :(得分:0)
您有几个答案描述了示例代码的问题,因此在此不再赘述。但是我认为可能有一种更简单的方法来获得您想要的东西,所以我将提供另一种选择。
如果您想从字典中选择随机项目,那么Key
并不重要(示例代码从不实际使用键,而是尝试选择随机值) ,您也可以改用List<T>
。幸运的是,字典具有这样的集合:名为Values
的属性,如果我们从字典中创建列表,则可以很容易地返回随机值:
// Code to populate dictionary omitted
// Return a random value from the dictionary's Values collection
return Questions.Values.ToList()[rand.Next(Questions.Count)];
如果由于某种原因您确实需要与该值相关联的键,那么我们可以对Keys
集合进行相同的操作,然后我们就可以了:
// Get a random key from the dictionary's Keys collection
var randomKey = Questions.Keys.ToList()[rand.Next(Questions.Count)];
// Use that key to get a value
return Questions[randomKey];