我正在做一个测验,其中有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
请注意,困难和话题是先前已声明为用户输入的字符串。
我的问题是事实,如果我选择容易的难度,例如,正确的答案不会是输出的两个答案之一。我需要确保正确的答案将总是被输出,其他答案将是随机的。
谢谢你的一切帮助。
在编码方面我并不是很有经验,所以我对我犯的任何错误都很愚蠢表示歉意。
您需要阅读本网站关于如何提问的常见问题。我们将帮助您处理不起作用的代码,但我们不会为您编写代码。 –
@SamAxe我编辑了我的问题,如果可能,您可以提供一些帮助吗? – Flame7427
@LarsTech我现在有,对不起, – Flame7427