2013-10-11 61 views
6

我正在創建一個小測驗控制檯應用程序。 我列出了3個問題。 我如何讓程序隨機選擇一個問題並在控制檯中打印出來?C#從列表中選擇隨機元素

我已經嘗試了一些不同的代碼,但似乎無法得到它的工作原因。 這是最後的代碼,我試過了,這是我從這個網站的其他用戶得到的,但我得到的錯誤:

"The name 'string' does not exists in current context" 

「自從'Quiz.Questions.main()返回void,返回關鍵字必須不能跟一個對象表達式」

這裏是我試過的代碼的最後一塊:

class Questions 
{ 
    public static void main() 
    { 
     var questions = new List<string>{ 
      "question1", 
      "question2", 
      "question3"}; 
     int index = Random.Next(strings.Count); 
     questions.RemoveAt(index); 
     return questions; 

    } 

}

謝謝大家的響應。 我已經通過創建一個數組而不是一個List來解決我的問題。 現在這是我的代碼:

class Questions 
{ 
    public static void main() 
    { 
     string[] questions = new string[3]; 
     questions[0] = "question1"; 
     questions[1] = "question2"; 
     questions[2] = "question3"; 
     Random rnd = new Random(); 
     Console.WriteLine(questions[rnd.Next(0,2)]); 
    } 
} 
+0

你怎麼稱呼你的方法是什麼?因爲它目前是一個無效類型,所以你不得不忽略返回值「questions」。var randomQuestion = questions [ – HimBromBeere

+3

] http://stackoverflow.com/questions/2019417/access-random-item-in-list – gleng

+1

var randomQuestion = questions [ new Random()。Next(questions.Count)];首先確保該列表不爲空 – iulian3000

回答

4

你需要的System.Console.WriteLine statment。

class Questions 
{ 
    public static void main() 
    { 
     var questions = new List<string>{ 
      "question1", 
      "question2", 
      "question3"}; 
     int index = Random.Next(questions.Count); 
     System.Console.WriteLine(questions[index]); 

    } 
} 
+0

也應該用新的Random()。下一個(...)將strings.Count替換爲questions.Count和Random.Next(...)。 –

+0

謝謝阿列克謝。糾正。 –

+0

@AlexeyMitev:'new Random()。Next'有一個缺陷,即如果它在短時間間隔內被重複調用,它將不會創建不同的數字。 –

1

「這個名字 '字符串' 不存在當前上下文存在」 我想,你希望

int index = random.Next(questions.Count); // according to the lower-case random see last paragraph 

,而不是

int index = Random.Next(strings.Count); 

由於是不可變的機智h名稱strings,無論如何你想刪除一個問題。

此外,您不能從void方法返回的東西。因此,創建一個返回列表:

private Random random = new Random(); 
List<string> GetRemoveQuestion(List<string> questions) 
{ 
     int index = random.Next(questions.Count); 
     questions.RemoveAt(index); 
     return questions; 
} 

編輯:最後但並非最不重要的,你不能使用Random.Next。這將假定在Random中有一個staticNext方法,情況並非如此。因此,我已經在上面展示瞭如何創建和使用實例。請注意,您不應該在方法本身創建它,因爲它的播種時間很長。如果你非常快地調用這個方法,你會經常得到相同的「隨機」值。

查看msdn at the remarks section瞭解更多詳情。

+1

非常確定隨機沒有靜態下一個方法 –

+0

@WeylandYutani:謝謝,相應地編輯了我的答案。 –

11

您確定要刪除問題並返回其餘問題嗎? 您是否應該選擇一個?像這樣的東西:

public static void main() 
{ 
    var random = new Random(); 
    var questions = new List<string>{ 
     "question1", 
     "question2", 
     "question3"}; 
    int index = random.Next(questions.Count); 
    Console.WriteLine(questions[index]); 
} 
0

你有幾個小錯誤。

您需要對Rand對象的引用。您正在查看strings而不是questions。您正在移除一個元素,而不是選擇它。

試試這個:

void Main() 
{ 
    Random rand = new Random(); 
    var questions = new List<string>{ 
     "question1", 
     "question2", 
     "question3"}; 
    int index = rand.Next(questions.Count); 
    return questions[index]; 
    // If you want to use Linq then 
    // return questions.Skip(index).Take(1); 
} 
0

像這樣的東西可能是你想要什麼:

private Random rng; 
T PickRandom<T>(List<T> list) 
{ 
    return list[rng.NextInt(list.Count)]; 
} 

你可以稱之爲你的名單上,以從中獲得一個隨機元素。

3

嘗試這樣的事情

public static void main() 
{ 
    var questions = new List<string>{ 
     "question1", 
     "question2", 
     "question3"}; 
    Random rnd = new Random(); 
    int index = rnd.Next(questions.Count) 
    string question = questions[index]; 
    questions.RemoveAt(index); // Are you sure you neex to remove? 

    System.Console.WriteLine(question); 
} 

有一個在你使用string代替questions其中一個錯字。另外,Random對象需要初始化。

1

對於其它搜受益:如果你想消減列表,這樣可以確保您使用的所有項目以隨機方式,然後執行以下操作:

//use the current time to seed random so it's different every time we run it 
Random rand = new Random(DateTime.Now.ToString().GetHashCode()); 
var list = new List<string>{ "item1", "item2", "item3"}; 

//keep extracting from the list until it's depleted 
while (list.Count > 0) { 
    int index = rand.Next(0, list.Count); 
    Console.WriteLine("Rand Item: " + list[index]); 
    list.RemoveAt(index); 
}