我正在做一个有3个难点的测验。对于每个难度,必须提供不同数量的答案。
因此,轻松必须有 2 答案,中等必须有 3 答案,而且必须有 4 答案。
我需要一种随机化答案位置的方法,例如,如果容易困难,那么只有 2 答案(其中一个是正确的答案) )或者如果难以困难,则会有 4 答案,其中一个是正确的。
注意:问题和答案已从csv文件中读取,结构为:
问题,正确答案,其他答案,其他答案,其他答案
正确答案始终位于csv文件中该行的第二个位置,因此在我的数组中,它将是 questions(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
请注意,难度和主题都是先前已声明的用户输入字符串。
我的问题是因为如果我选择简单难度,例如,正确的答案将不是输出的两个答案之一。我需要确保输出正确的答案始终,其他答案将是随机的。
感谢您的帮助。
我在编码方面不是很经验,所以我为我所犯的任何愚蠢错误道歉。
答案 0 :(得分:0)
尝试:
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
错误的答案。这将保证正确答案,包括ansnum - 1
随机错误答案,然后随机播放所有答案。
答案 1 :(得分: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