Ich versuche, diesen Code zu bekommen, um 4 Schlüssel nach dem Zufallsprinzip auszuwählen, und schreibe sie dann mit einer 'foreach'-Schleife in die Konsole. Anstatt für jede Iteration eine zufällige Auswahl auszuwählen, wählt sie nur zufällig einen der Schlüssel aus und schreibt ihn viermal auf die Konsole. Hier ist der Code:Warum wird diese Methode nur einmal randomisiert, anstatt jede Iteration zufällig zu setzen?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ListPractice
{
public class DictionaryData
{
public static void GetRandomKey()
{
Dictionary<string, int[]> QandA_Dictionary = new Dictionary<string, int[]>();
QandA_Dictionary.Add("What is 1 + 1?", new int[] { 1, 2, 3, 4 });
QandA_Dictionary.Add("What is 1 + 2?", new int[] { 1, 2, 3, 4 });
QandA_Dictionary.Add("What is 1 + 3?", new int[] { 1, 2, 3, 4 });
QandA_Dictionary.Add("What is 1 + 4?", new int[] { 2, 3, 4, 5 });
List<string> keys = new List<string>(QandA_Dictionary.Keys);
foreach (var element in keys)
{
Random rand = new Random();
string randomKey = keys[rand.Next(keys.Count)];
Console.WriteLine(randomKey);
}
Console.ReadKey();
}
public static void Main(string[] args)
{
GetRandomKey();
}
}
}
Das ist ziemlich interessant. Ich lerne noch. Danke für Ihre Hilfe. – sean
@sean: Kein Problem, ich bin froh, dass ich dir helfen konnte! Es gibt noch mehr Informationen zu lesen [** die MSDN-Dokumentation darüber **] (https://msdn.microsoft.com/en-us/library/h343ddh9 (v = vs.110) .aspx). –