我的程序为用户提供了一个基于excel文件的测验。问题以随机顺序出现。在excel文件的每一行中有5个可能的问题(n,3-7),答案总是在该行的第二个单元格中(n,2)。有135行,但前两行与问题无关。用户可以正确回答问题,他们应该尽量在时限内回答尽可能多的问题,因此当时间到了时,用户将永远不会看到未经询问的问题。我需要帮助的问题是有一个难得的机会(665中的1)可以重复一个问题。我怎样才能防止这一点? (另外,我很新的节目)的问题生成阅读之前未读过的随机excel单元格
Private Sub newquestion()
'New Question
Randomize()
row = CInt(rnd.Next(3, 135))
key = CInt(rnd.Next(3, 7))
lblgametype.Text = "Guess the answer from the hint"
lblquestion.Text = worksheet.Cells(row, key).Value
End Sub
代码检查答案
Private Sub OnKeyDownHandler(ByVal sender As Object, ByVal e As KeyEventArgs) Handles txtanswer.KeyDown
'Prevent that annoying ass windows ding sound
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
End If
If e.KeyCode = Keys.Escape Then
e.SuppressKeyPress = True
End If
If e.KeyCode = Keys.Enter Then 'If the user presses Enter while txtanswer is selected...
userguess = txtanswer.Text 'Assign the text the user enters to the global userguess variable
If userguess.ToUpper = worksheet.Cells(row, 2).Value.ToString.ToUpper Then
'User answers a question correct
lblcorrect.ForeColor = Color.Green
lblcorrect.Text = "Correct. +1"
txtanswer.Text = ""
userguess = ""
abilityscore += 1
lblscore.Text = "Score: " & abilityscore
If abilityscore > abilityhighscore Then
abilityhighscore = abilityscore
End If
newquestion()
Else
'User answers a question incorrectly
lblcorrect.ForeColor = Color.Red
lblcorrect.Text = "incorrect."
txtanswer.Text = ""
End If
End If
If e.KeyCode = Keys.Escape Then 'If the user presses escape while txtanswer is selected...
btnskip.PerformClick() 'Treat it as if they pressed skip
End If
End Sub
规范问题跳过
Private Sub btnskip_Click(sender As Object, e As EventArgs) Handles btnskip.Click
Me.TargetDT = Me.TargetDT.Subtract(TimeSpan.FromSeconds(15)) 'Subtract 15 seconds from the timer
txtanswer.Focus() 'Reset focus back to the textbox
newquestion()
End Sub
的问题是随机的,并不意味着唯一的。你需要一个shuffle:见[选择唯一的随机数字](http:// stackoverflow。com/q/35120454/1070452) – Plutonix
@Plutonix我读过你对这个家伙问题的回答,我相信我要找的答案是在答案的“随机对”部分。我从来没有使用过列表或数组。你可以告诉我,我的代码会是什么样子?另外,感谢您的帮助。 – Purin
如果你的测验有5个问题:'rowNums = Enumerable.Range(3,135).OrderBy(Function(r)RNG.Next())。拿(5).ToArray()'来保存所有5个x1行号为这个游戏。这件东西就像一个集中型游戏。 'RNG'将是一个NET的'Random'对象,而不是传统的VB'Rnd'函数 – Plutonix