我的程序爲用戶提供了一個基於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