2017-09-29 33 views
0

我正在做一个测验,其中有3个困难。对于每个难题,必须有不同数量的答案。VB.net控制台随机答案的位置

所以,易必须答案,中必须有解答和硬必须答案。

我需要一种方法来随机答案位置,例如,如果是容易难度只会有答案(其中一个是正确的),或者如果它是困难会有的答案,其中之一是正确的。

注意:这些问题和答案已经从一个CSV文件读取的结构:

问题,正确答案,对方的回答,对方的回答,对方的回答

正确回答始终处于csv文件行中的第二个位置,因此在我的数组中,它将是问题(1)

我当前的代码是:

Dim topic as String = "music" ' <- An example topic for the quiz 
Dim filereaderq As New StreamReader(topic & ".csv", True) 
    While filereaderq.EndOfStream = False 
     questions = filereaderq.ReadLine.Split(",") 

     If difficulty.ToLower = "easy" Or difficulty = "1" Then 
      ansnum = 2 
      difficulty = "e" 
     ElseIf difficulty.ToLower = "medium" Or difficulty = "2" Then 
      ansnum = 3 
      difficulty = "m" 
     ElseIf difficulty.ToLower = "hard" Or difficulty = "3" Then 
      ansnum = 4 
      difficulty = "h" 
     End If 

     ' Random Number 
     Dim answerc As New List(Of Integer) 
     answerc.Add(0) 

     or i = 0 To ansnum 
      Dim ok As Boolean = False 
      Do 
       Dim num As Integer = Int((ansnum * Rnd()) + 1) 
       If answerc.Contains(num) Then 
        ok = False 
       Else 
        ok = True 
        answerc.Add(num) 
       End If 
      Loop Until ok 
     Next 

     Console.WriteLine("----------------------------") 
     Console.WriteLine("Q" + CStr(qnum) + ". " + questions(0)) 
     qnum = qnum + 1 
     Console.WriteLine("") 

     If difficulty = "e" Then 
      Console.WriteLine("1. " + questions(answerc(1))) 
      Console.WriteLine("2. " + questions(answerc(2))) 
     ElseIf difficulty = "m" Then 
      Console.WriteLine("1. " + questions(answerc(1))) 
      Console.WriteLine("2. " + questions(answerc(2))) 
      Console.WriteLine("3. " + questions(answerc(3))) 
     ElseIf difficulty = "h" Then 
      Console.WriteLine("1. " + questions(answerc(1))) 
      Console.WriteLine("2. " + questions(answerc(2))) 
      Console.WriteLine("3. " + questions(answerc(3))) 
      Console.WriteLine("4. " + questions(answerc(4))) 
     End If 

     Console.WriteLine("----------------------------") 
     Console.Write("Answer Number: ") 
     ansnum = Console.ReadLine() 

     If answerc(0) = ansnum Then 
      score = score + 1 
     Else 
      score = score 
     End If 

    End While 

请注意,困难话题是先前已声明为用户输入的字符串。

我的问题是事实,如果我选择容易的难度,例如,正确的答案不会是输出的两个答案之一。我需要确保正确的答案将总是被输出,其他答案将是随机的。

谢谢你的一切帮助。

在编码方面我并不是很有经验,所以我对我犯的任何错误都很愚蠢表示歉意。

+1

您需要阅读本网站关于如何提问的常见问题。我们将帮助您处理不起作用的代码,但我们不会为您编写代码。 –

+0

@SamAxe我编辑了我的问题,如果可能,您可以提供一些帮助吗? – Flame7427

+0

@LarsTech我现在有,对不起, – Flame7427

回答

0

尝试:您要ansnum - 1错误的答案

Dim answerc = Enumerable.Concat({ 0 }, Enumerable.Range(2, 3).Take(ansnum - 1).OrderBy(Function (x) Rnd()).Concat({ 1 }).OrderBy(Function (x) Rnd())).ToList() 

。这将保证正确的答案,包括ansnum - 1随机错误的答案,然后洗牌的所有答案。

+0

如何指定我想要的错误答案数量?尝试先前声明的整数,但得到此消息:**类型'列表(字符串)'的值不能转换为'列表(整数)'** – Flame7427

+0

@ Flame7427 - 您没有指定什么'answersFromCsv' (我选择的名字)是什么类型。你需要提供[mcve]以获得更具体的答案。尽管如此,我的答案中的值“n”表示您得到了多少错误答案。 – Enigmativity

+0

我已将代码更新为最小 – Flame7427

0

好的,这里是一些代码,包括正确的答案,并随机选择错误的答案和它们出现的顺序。

Private Sub Quiz(strMode As String) 
     Dim arrQuiz As String() = {"Question", "Correct Answer", "Wrong Answer1", "Wrong Answer2", "Wrong Answer3"} 
     Dim intIndex As Integer 
     Dim lstWrongAnswers As New List(Of String) 
     Dim lstAllAnswers As New List(Of String) 
     Dim strAnswerChoice As String = arrQuiz(1) 
     lstWrongAnswers.Add(arrQuiz(2)) 
     lstWrongAnswers.Add(arrQuiz(3)) 
     lstWrongAnswers.Add(arrQuiz(4)) 
     lstAllAnswers.Add(arrQuiz(1)) 
     Randomize() 'Before calling Rnd, use the Randomize statement without argument to initialize the 
     '   Random-Number generator with a seed based on the system timer. 

     Select Case strMode 
      Case "Easy" 
       'Choose 1 wrong answer 
       intIndex = CInt(Int(lstWrongAnswers.Count * Rnd())) 
       'select a ramdom index using the Count method of the List 
       lstAllAnswers.Add(lstWrongAnswers.Item(intIndex)) 
       'The Rnd function returns a value less than 1, but greater than or equal to zero. 
       intIndex = CInt(Int(lstAllAnswers.Count * Rnd())) 
       'Int returns the integer portion of the number (always rounds down) 
       Dim FirstDisplayAnswer As String = lstAllAnswers.Item(intIndex) 
       lstAllAnswers.RemoveAt(intIndex) 
       Dim SecondDisplayAnswer As String = lstAllAnswers.Item(0) 
       MessageBox.Show($"The Question is {arrQuiz(0)}{vbCrLf}The first choice is {FirstDisplayAnswer}{vbCrLf}The second choice is {SecondDisplayAnswer}") 
      Case "Medium" 
       'choose 2 wrong answers 
       intIndex = CInt(Int(lstWrongAnswers.Count * Rnd())) 
       'Option Strict is not satisfied with Int, It requires CInt so I need both 
       lstAllAnswers.Add(lstWrongAnswers.Item(intIndex)) 
       'use the Item method of the List to retrieve the value at that index 
       lstWrongAnswers.RemoveAt(intIndex) 
       intIndex = CInt(Int(lstWrongAnswers.Count * Rnd())) 
       lstAllAnswers.Add(lstWrongAnswers.Item(intIndex)) 
       intIndex = CInt(Int(lstAllAnswers.Count * Rnd())) 
       Dim FirstAnswer As String = lstAllAnswers.Item(intIndex) 
       lstAllAnswers.RemoveAt(intIndex) 
       intIndex = CInt(Int(lstAllAnswers.Count * Rnd())) 
       Dim SecondAnswer As String = lstAllAnswers.Item(intIndex) 
       lstAllAnswers.RemoveAt(intIndex) 
       intIndex = CInt(Int(lstAllAnswers.Count * Rnd())) 
       Dim ThirdAnswer As String = lstAllAnswers.Item(intIndex) 
       MessageBox.Show($"Question is {arrQuiz(0)}{vbCrLf}a: {FirstAnswer}{vbCrLf}b: {SecondAnswer}{vbCrLf}c: {ThirdAnswer}") 
      Case "Difficult" 
       'shuffle all answers 
       Dim arrDisplay(3) As String 
       lstAllAnswers.Add(arrQuiz(2)) 
       lstAllAnswers.Add(arrQuiz(3)) 
       lstAllAnswers.Add(arrQuiz(4)) 
       For x As Integer = 0 To 3 
        intIndex = CInt(Int(lstAllAnswers.Count * Rnd())) 
        arrDisplay(x) = lstAllAnswers.Item(intIndex) 
        lstAllAnswers.RemoveAt(intIndex) 
       Next 
       MessageBox.Show($"{arrQuiz(0)}{vbCrLf}a: {arrDisplay(0)}{vbCrLf}b: {arrDisplay(1)}{vbCrLf}c: {arrDisplay(2)}{vbCrLf}d: {arrDisplay(3)}") 
      Case Else 
        '?? 
     End Select 

    End Sub